Sorting functions in PHP

And the Spaceship Operator πŸš€πŸš€πŸš€

List

A list is an ordered collection.
The order is sequential starting from 0 and increasing 1 by 1 for each item.
It is also possible to have duplicated items in a list.

$list = ['Croatia', 'Belgium', 'Austria', 'Belgium', 'Denmark'];
print_r($list);

Array
(
[0] => Croatia
[1] => Belgium
[2] => Austria
[3] => Belgium
[4] => Denmark
)

Map (also known as Dictionaries)

A map is an unordered collection.
Each element is composed of a key and a value.
It is not possible to have the same key more than once.

$map = [
'Croatia' => 0,
'Belgium' => 1,
'Austria' => 2,
'Denmark' => 3,
'Belgium' => 4,
];
print_r($map);

Array
(
[Croatia] => 0
[Belgium] => 4 // 'Belgium => 1' was overriden
[Austria] => 2
[Denmark] => 3
)

R = reverse ➝ sort in descencing order, lists & maps
K = key ➝ sort by key, only for maps
A = associative ➝ sort by value, only for maps
U = user-defined ➝ defined by the user in a callback

Sorting Lists 🧡

sort() & rsort()

These methods sort lists in ascending/descending order.

# sort: Sort list by ascending order.
$sort = ['Croatia', 'Belgium', 'Austria', 'Belgium'];
sort($sort);

Array
(
[0] => Austria
[1] => Belgium
[2] => Belgium
[3] => Croatia
)

---

# rsort: Sort list by descending order.
$rsort = ['Croatia', 'Belgium', 'Austria', 'Belgium'];
rsort($rsort);

Array
(
[0] => Croatia
[1] => Belgium
[2] => Belgium
[3] => Austria
)

Sorting Maps πŸ—ΊοΈ

asort(), arsort(), ksort() & krsort()

These methods sort associative arrays by key/value in ascending/descending order.

# ksort: Sort map by key in ascending order.
$ksort = [100 => 'Croatia', 200 => 'Austria', 300 => 'Belgium'];
ksort($ksort);

Array
(
[100] => Croatia
[200] => Austria
[300] => Belgium
)

---

# krsort: Sort map by key in descending order.
$krsort = [100 => 'Croatia', 200 => 'Austria', 300 => 'Belgium'];
krsort($krsort);

Array
(
[300] => Belgium
[200] => Austria
[100] => Croatia
)

---

# asort: Sort map by value in ascending order.
$asort = [100 => 'Croatia', 200 => 'Austria', 300 => 'Belgium'];
asort($asort);

Array
(
[200] => Austria
[300] => Belgium
[100] => Croatia
)

---

# arsort: Sort map by value in descending order.
$arsort = [100 => 'Croatia', 200 => 'Austria', 300 => 'Belgium'];
arsort($arsort);

Array
(
[100] => Croatia
[300] => Belgium
[200] => Austria
)

User-defined functions πŸ‘©β€πŸ’»

Until now everything was nice, but as you know, usually we have a collection of complex objects, and sometimes we want to define our own sorting function, in these scenarios, the asort() method simply doesn’t work. Eg:

/** @var callable(mixed,mixed):int $callable */
$callable = function (mixed $a, mixed $b): int { ... }

usort(&$array, $callable): bool
function compare(int $a, int $b): int
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}

# Is the same than...

function compare(int $a, int $b): int
{
return $a <=> $b;
}

# Is the same than...

fn (int $a, int $b): int => $a <=> $b;

usort(), uksort() & uasort()

These methods sort by keys or values (preserving or resetting the keys) in a user-defined callback. Let’s see some examples:

# usort: Sort list/map by callback from the values, keys are reset.
$usort = [100 => 'Croatia', 300 => 'Austria', 200 => 'Belgium'];
usort($usort, fn (string $a, string $b): int => $a <=> $b);

Array
(
[0] => Austria
[1] => Belgium
[2] => Croatia
)

---

# uksort: Sort map by callback from the keys.
$uksort = [100 => 'Croatia', 300 => 'Austria', 200 => 'Belgium'];
uksort($uksort, fn (int $a, int $b): int => $a <=> $b);

Array
(
[100] => Croatia
[200] => Belgium
[300] => Austria
)

---

# uasort: Sort map by callback from the values preserving the keys.
$uasort = [100 => 'Croatia', 300 => 'Austria', 200 => 'Belgium'];
uasort($uasort, fn (string $a, string $b): int => $a <=> $b);

Array
(
[300] => Austria
[200] => Belgium
[100] => Croatia
)

πŸ“‘ Cheat Sheet

sort() - sort list in ascending order
rsort() - sort list in descending order

asort() - sort map by value in ascending order
ksort() - sort map by key in ascending order
arsort() - sort map by value in descending order
krsort() - sort map by key in descending order

usort() - sort list or map by value in a user-defined callback, reset keys
uksort() - sort map by keys in a user-defined callback
uasort() - sort map by value in a user-defined callback

Reference

--

--

Competitive, entrepreneur and autodidact. Hard worker, lover of technology and free software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesus Valera Reales

Competitive, entrepreneur and autodidact. Hard worker, lover of technology and free software.