Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP sort() Function

PHP sort() Function

The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. It sorts the actual array and hence changes are reflected in the original array itself. The function provides us with 6 sorting types, according to which the array can be sorted.

Syntax:

bool sort($array, sorting_type)

Parameters:

  1. $array – The parameter specifies the array which we want to sort. It is a mandatory parameter
  2. sorting_type – This is an optional parameter. There are 6 sorting types which are described below:
    • SORT_REGULAR – When we pass 0 or SORT_REGULAR in the sorting_type parameter, the items in the array are compared normally.
    • SORT_NUMERIC – When we pass 1 or SORT_NUMERIC in the sorting_type parameter, the items in the array are compared numerically
    • SORT_STRING – When we pass 2 or SORT_STRING in the sorting_type parameter, the items in the array are compared string-wise
    • SORT_LOCALE_STRING – When we pass 3 or SORT_LOCALE_STRING in the sorting_type parameter, the items in the array are compared as string based on current locale
    • SORT_NATURAL – When we pass 4 or SORT_NATURAL in the sorting_type parameter, the items in the array are compared as string using natural ordering
    • SORT_FLAG_CASE – When we pass 5 or SORT_FLAG_CASE in the sorting_type parameter, the items in the array are compared as strings. The items are treated as case-insensitive and then compared. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING.

Return Value: It returns a boolean value, TRUE on success and False in failure. It sorts the original array in ascending order which is passed as a parameter.

Examples:

Input : $array = [3, 4, 1, 2] 
Output : 
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Input : $array = ["neveropen2", "raj1", "striver3", "coding4"]
Output :
Array
(
    [0] => coding4
    [1] => neveropen2
    [2] => raj1
    [3] => striver3
)

Below programs illustrate the sort() function in PHP:

Program 1: Program to demonstrate the use of sort() function.




<?php
// PHP program to demonstrate the use of sort() function
  
$array = array(3, 4, 2, 1);
  
// sort function 
sort($array); 
  
// prints the sorted array 
print_r($array);
?>


Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Program 2 : Program to demonstrate the use of sort() function to sort the string case-sensitively.




<?php
// PHP program to demonstrate the use of sort() function
// sorts the string case-sensitively 
$array = array("neveropen", "Raj", "striver", "coding", "RAj");
  
// sort function, sorts the string case-sensitively 
sort($array, SORT_STRING); 
  
// prints the sorted array 
print_r($array);
?>


Output:

Array
(
    [0] => RAj
    [1] => Raj
    [2] => coding
    [3] => neveropen
    [4] => striver
)

Program 3 : Program to demonstrate the use of sort() function to sort the string case-insensitively.




<?php
// PHP program to demonstrate the use
// of sort() function sorts the string 
// case-insensitively 
$array = array("neveropen", "Raj", "striver", "coding", "RAj");
  
// sort function, sorts the
// string case-insensitively 
sort($array, SORT_STRING | SORT_FLAG_CASE); 
  
// prints the sorted array 
print_r($array);
?>


Output:

Array
(
    [0] => coding
    [1] => neveropen
    [2] => Raj
    [3] => RAj
    [4] => striver
)

Reference:
http://php.net/manual/en/function.sort.php

RELATED ARTICLES

Most Popular

Recent Comments