Categories
PHP

Sorting with user-defined comparison function

The sorting functions, described in previous tutorials, sort elements in alphabetic, numeric, or alphanumeric order. To sort elements based on user-defined criteria, PHP provides three functions: usort, uasort, and uksort.

  1. usort() sorts the array based on the value of each element
  2. uasort() sorts the array based on the value of each element and preserves the key/value associations
  3. uksort() rearranges the elements based on the key of each element.
usort (array &$array, callable $callback): bool
uasort(array &$array, callable $callback): bool
uksort(array &$array, callable $callback): bool

When these functions sort the array, the user-defined callback function is called to determine if one element is greater than, lesser than, or equal to another. The callback function can be written to implement any sort order, but the function must conform to the prototype:

callback_function(mixed $a, mixed $b): integer

The callback function takes two arguments, a and b, and returns negative integer value if a is less than b, returns positive integer value if a is greater than b, and 0 if a and b are equal. How the function determines that one value is less than, greater than, or equal to another depends on the requirements of sorting.

usort

The following example shows how usort( ) sorts an array of strings based on the length of each string, it uses strlen() function to determine the length of the $a and $b:

<?php
 // Compare two string values based on the length
 function cmp_length($a, $b) {
  //The strlen returns the length of a string.
  return strlen($a) - strlen($b);
 }

 $animals = ['cow', 'ox', 'horse', 'fox', 'lion'];
 usort($animals, 'cmp_length');
 print_r($animals);

/*Prints:
Array ( [0] => ox [1] => cow [2] => fox [3] => lion [4] => horse )
*/

In this example, cmp_length( ) is defined as the callback function, but it isn’t called directly by the script. The name of the function, cmp_length, is passed as an argument to usort( ), and usort( ) uses cmp_length( ) as part of the sorting algorithm.

The callback function cmp_length used there is a very simple way to do a numeric sorting. By subtracting the two values, the function returns the desired values: A positive number if the first parameter is larger than the second one, 0 if both parameters are equal, and a negative number otherwise.

User-defined functions used in this way are often referred to as callback functions.

uasort

Sort an array with a user-defined comparison function and preserves the key/value associations. See the following example:

<?php
 // Compare two string values based on the length
 function cmp_length($a, $b) {
  //The strlen returns the length of a string.
  return strlen($a) - strlen($b);
 }

 $animals = ['cow', 'ox', 'horse', 'fox', 'lion'];
 uasort($animals, 'cmp_length');
 print_r($animals);

/*Prints:
Array ( [1] => ox [0] => cow [3] => fox [4] => lion [2] => horse )
*/

 $arr = ['a' => 'cow', 'b' => 'ox',
         'c' => 'horse', 'd' => 'fox',
         'e' => 'lion'];
 uasort($arr, 'cmp_length');
 print_r($arr);

/*Prints:
Array ( [b] => ox [a] => cow [d] => fox [e] => lion [c] => horse )
*/

uksort

This function sorts an array in ascending order by keys with a user-defined comparison (callback) function. Similar to usort and uasort, the callback function must determine what is equal, what is greater than, and what is less than. The keys/values association from the original array remains intact.

<?php
 // Compare two string values based on the length
 function cmp_length($a, $b) {
  //The strlen returns the length of a string.
  return strlen($a) - strlen($b);
 }

 $arr = ['cows' => 5, 'ox' => 1,
         'horses' => 2, 'fox' => 0,
         'lion' => 0];
 uksort($arr, 'cmp_length');
 print_r($arr);
 //Array ( [ox] => 1 [fox] => 0 [cows] => 5 [lion] => 0 [horses] => 2 )

More Posts on PHP Sorting Arrays: