Differential analysis of ranking functions sort asort rsort krsort and ksort in PHP

  • 2021-07-13 04:35:30
  • OfStack

The sort () function is used to sort array cells from low to high.
The rsort () function is used to sort array cells from high to low.
The asort () function is used to sort array cells from low to high and maintain index relationships.
The arsort () function is used to sort array cells from high to low and maintain index relationships.
The ksort () function is used to sort array cells from low to high by key name.
The krsort () function is used to sort array cells from high to low by key name.

sort ()

The PHP sort () function sorts array cells from low to high, returning TRUE on success and FALSE on failure.
Note: This function assigns new key names to the cells in the ordered array, which deletes the old key names instead of just reordering them.
Syntax:
bool sort (array & array [, int sort_flags] )
The optional parameter sort_flags is used to change the behavior of sorting:
sort_flags Value Description
SORT_REGULAR Normal Comparison Unit
SORT_NUMERIC cells are compared as numbers
SORT_STRING cells are compared as strings
SORT_LOCALE_STRING compares cells as strings based on the current region (locale) setting

Examples:


    
    $arr = array("b", "a", "c");
    sort($arr);
    print_r($arr);
    ?>

Run the example output:


    Array ( [0] => a [1] => b [2] => c )

In this example, the $arr array cells are sorted alphabetically, and after the array cells are sorted, the key values are reassigned.

rsort ()
The PHP rsort () function behaves in contrast to sort () and sorts array cells from high to low, as used by the sort () function.

asort ()
The PHP asort () function is used to sort array cells from low to high and maintain index relationships, returning TRUE on success and FALSE on failure.
Syntax:
bool asort (array & array [, int sort_flags] )
The optional parameter sort_flags is used to change the sorting behavior, see sort () for details.
Examples:
  


    $arr = array("b", "a", "c");
    asort($arr);
    print_r($arr);
    ?>

Run the example output:


    Array ( [1] => a [0] => b [2] => c )

arsort ()
The PHP arsort () function behaves in contrast to asort (), ordering array cells from top to bottom and maintaining index relationships, as used by the asort () function.

ksort ()
The PHP ksort () function sorts array cells from low to high by key name, returning TRUE on success and FALSE on failure.
This function preserves the original key name, so it is often used in associative arrays.
Syntax:
bool ksort (array & array [, int sort_flags] )
The optional parameter sort_flags is used to change the sorting behavior, see sort () for details.
Examples:
  
  


$arr = array("b"=>18, "a"=>20, "c"=>25);
    ksort($arr);
    print_r($arr);
    ?>

Run the example output:


    Array ( [a] => 20 [b] => 18 [c] => 25 )

krsort ()
The PHP krsort () function behaves in contrast to ksort () and sorts array cells from high to low by key name, as used by the ksort () function.


Related articles: