Sort

PHP Sort Function

Customize sort

usort()

usort(array &$array, callable $callback): bool

Sort an array by values using a user-defined comparison function

function cmp($first_value, $second_value)
{
    if ($first_value == $second_value) {
        return 0;
    }
    return ($first_value < $second_value) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

var_dump($a);
// array(5) {
//   [0]=>
//   int(1)
//   [1]=>
//   int(2)
//   [2]=>
//   int(3)
//   [3]=>
//   int(5)
//   [4]=>
//   int(6)
// }
return value description
0 $first_value and $second_value are equal, keep the original sort
-1 $first_value is smaller than $second_value, keep the original sort
1 $first_value is bigger than $second_value will need to change the sort, move the smaller value $second_value to the lower order

uksort()

uksort(array &$array, callable $callback): bool

Sort an array by keys using a user-defined comparison function

uasort()

uasort(array &$array, callable $callback): bool

Sort an array with a user-defined comparison function and maintain index association

Sort Value

sort()

sort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array in ascending order

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);

var_dump($fruits);
// array(4) {
//   [0]=>
//   string(5) "apple"
//   [1]=>
//   string(6) "banana"
//   [2]=>
//   string(5) "lemon"
//   [3]=>
//   string(6) "orange"
// }

rsort()

rsort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array in descending order

$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);

var_dump($fruits);
// array(4) {
//   [0]=>
//   string(6) "orange"
//   [1]=>
//   string(5) "lemon"
//   [2]=>
//   string(6) "banana"
//   [3]=>
//   string(5) "apple"
// }

asort()

asort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array in ascending order and maintain index association

$fruits = array(
    "d" => "lemon",
    "a" => "orange",
    "b" => "banana",
    "c" => "apple"
);
asort($fruits);

var_dump($fruits);
// array(4) {
//   ["c"]=>
//   string(5) "apple"
//   ["b"]=>
//   string(6) "banana"
//   ["d"]=>
//   string(5) "lemon"
//   ["a"]=>
//   string(6) "orange"
// }

arsort()

arsort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array in descending order and maintain index association

$fruits = array(
    "d" => "lemon",
    "a" => "orange",
    "b" => "banana",
    "c" => "apple"
);
arsort($fruits);

var_dump($fruits);
// array(4) {
//   ["a"]=>
//   string(6) "orange"
//   ["d"]=>
//   string(5) "lemon"
//   ["b"]=>
//   string(6) "banana"
//   ["c"]=>
//   string(5) "apple"
// }

natsort()

natsort(array &$array): bool

Sort an array using a "natural order" algorithm

$array1 = $array2 = array(
    'IMG0.png', 
    'img12.png', 
    'img10.png', 
    'img2.png', 
    'img1.png', 
    'IMG3.png'
);

sort($array1);
echo "Standard sorting\n";
print_r($array1);

// Array
// (
//     [0] => IMG0.png
//     [1] => IMG3.png
//     [2] => img1.png
//     [3] => img10.png
//     [4] => img12.png
//     [5] => img2.png
// )

natsort($array2);
echo "\nNatural order sorting\n";
print_r($array2);

// Array
// (
//     [0] => IMG0.png
//     [5] => IMG3.png
//     [4] => img1.png
//     [3] => img2.png
//     [2] => img10.png
//     [1] => img12.png
// )

natcasesort()

natcasesort(array &$array): bool

Sort an array using a case insensitive "natural order" algorithm

$array1 = $array2 = array(
    'IMG0.png', 
    'img12.png', 
    'img10.png', 
    'img2.png', 
    'img1.png', 
    'IMG3.png'
);

sort($array1);
echo "Standard sorting\n";
print_r($array1);

// Array
// (
//     [0] => IMG0.png
//     [1] => IMG3.png
//     [2] => img1.png
//     [3] => img10.png
//     [4] => img12.png
//     [5] => img2.png
// )

natcasesort($array2);
echo "\nNatural order sorting (case-insensitive)\n";
print_r($array2);

// Array
// (
//     [0] => IMG0.png
//     [4] => img1.png
//     [3] => img2.png
//     [5] => IMG3.png
//     [2] => img10.png
//     [1] => img12.png
// )

Sort Key

ksort()

ksort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array by key in ascending order

$fruits = array(
    "d" => "lemon",
    "a" => "orange",
    "b" => "banana",
    "c" => "apple"
);
ksort($fruits);

var_dump($fruits);
// array(4) {
//   ["a"]=>
//   string(6) "orange"
//   ["b"]=>
//   string(6) "banana"
//   ["c"]=>
//   string(5) "apple"
//   ["d"]=>
//   string(5) "lemon"
// }

krsort()

krsort(array &$array, int $flags = SORT_REGULAR): bool

Sort an array by key in ascending order

$fruits = array(
    "d" => "lemon",
    "a" => "orange",
    "b" => "banana",
    "c" => "apple"
);
krsort($fruits);

var_dump($fruits);
// array(4) {
//   ["d"]=>
//   string(5) "lemon"
//   ["c"]=>
//   string(5) "apple"
//   ["b"]=>
//   string(6) "banana"
//   ["a"]=>
//   string(6) "orange"
// }

Sort flag

Flag Description
SORT_REGULAR compare items normally; the details are described in the comparison operators section
SORT_NUMERIC compare items numerically
SORT_STRING compare items as strings
SORT_LOCALE_STRING compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
SORT_NATURAL compare items as strings using "natural ordering" like natsort()
SORT_FLAG_CASE can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Reference