PHP sort() Function

PHP sort() Function

The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest.

Syntax

sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) 

Parameter

array(required)- This parameter represents the input array.

sort_flags (optional)- This parameter is used to modify the behavior of the sort. The various possible values for sorting type flags are as follows:

  • SORT_REGULAR (default) – It is used to compare items normally (don't change types)
  • SORT_NUMERIC – This flag is used to compare the items numerically.
  • SORT_STRING – It compares the given items as strings.
  • SORT_LOCALE_STRING – This flag compares the items as strings, based on the current locale.
  • SORT_NATURAL – It is used to compare items as strings using "natural ordering" like natsort().
  • SORT_FLAG_CASE – It can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort the given strings case-insensitively

 Return

This parameter returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema", "Age"=>"23","Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array from lowest to highest order
 sort($array);
 //printing the sorted array
 echo("\nSorted array: \n"); 
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 )
 Sorted array:  
 Array
 (
     [0] => 23
     [1] => India
     [2] => Reema
 ) 

Example 2

"Reema", "Age"=>23,"Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array from lowest to highest order
 sort($array );
 //printing the sorted array 
 echo("\nSorted array without second parameter: \n");
 print_r($array);
 //sorting array with second parameter as SORT_STRING 
 sort($array,SORT_STRING  );
 //printing the sorted array
 echo("\nSorted array with second parameter: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 )
 Sorted array without second parameter: 
 Array 
 (
     [0] => India
     [1] => Reema
     [2] => 23
 )
 Sorted array with second parameter: 
 Array
 ( 
     [0] => 23
     [1] => India
     [2] => Reema
 ) 

Example 3

"mango", 2 => "orange", 3 => "lichi", "apple");
 //sorting the array from lowest to highest order
 sort($fruits);
 echo("Sorted Array: \n");
 //printing the sorted values
 foreach ($fruits as $key => $val) {
     echo "$key = $val\n";
 }
 ?> 

Output

Sorted Array: 
 0 = apple
 1 = lichi
 2 = mango
 3 = orange 

Example 4

 array(1 => "mango", 2 => "banana", 2 => "apple"),
     "numbers" => array(1, 2, 3, 4, 5, 6),
     "values"   => array("first", 5 => "second", "third")
 );
 //sorting the array
 sort($array);
 //printing the reversed array
 print_r($array);
 ?> 

Output

Array
 (
     [0] => Array
         (
             [1] => mango
             [2] => apple
         )
     [1] => Array 
         (
             [0] => first
             [5] => second
             [6] => third
         )
     [2] => Array 
         (
             [0] => 1
             [1] => 2
             [2] => 3
             [3] => 4
             [4] => 5
             [5] => 6
         )
 )