UASORT, UKSORT and USORT - Sorting Functions in PHP
November 12, 2021
PHP offers lots of sorting functions to sort arrays. We're going to focus on these three great little beauties: usort, uasort and uksort.
Wow, just wow I love these three little functions.
Have you ever had an array of objects where you had to sort by one of the properties of the object (like the date created property)? Consider this scenario, the client asks that you sort all the blog posts by the date they were created. No big deal right?
Without built-in sorting functions, it could be a messy deal.
The usort, uksort, and uasort functions allow you to not think about "how" to rearrange the array of items &mdash, it will do that work for you. All you need to do is write up the code to compare the values you're looking to compare.
The U in usort, uasort and uksort stands for "user defined" to indicate that you're giving PHP a comparison function to use against the array.
USORT: Sorting an Array by Values
You can use usort, which will sort your values using a comparison function you write up. usort will take the array as the first parameter, and your function as the second parameter. It takes the array by reference, meaning the work will be done directly on the actual array.
In the example below I'm sorting through an array of dates, ordering them by date created in descending order.
$items = ['2021-01-15', '2021-05-13', '2021-06-12', '2020-10-31', '2021-02-14', '2020-03-17'];
usort($items, function ($a, $b) {
$time1 = strtotime($a);
$time2 = strtotime($b);
return ($time1 > $time2) ? -1 : 1;
});
print_r($items);
// 2021-06-12, 2021-05-13, 2021-02-14, 2021-01-15, 2020-10-31, 2020-03-17
As you can see, usort is able to compare each value of the array and rearrange the array. All you need to do is write the code that compares two values and return -1 or 1 (or 0 if both are equal) so that PHP can re-order the value within the array.
UKSORT: Sorting an Array by Keys, but Keeping the Values in Place
The uksort method will sort the keys of your array, keeping the values associated to each key intact. uksort accepts two arguments: the first being your array, and the second being your custom function which will perform the comparison between keys.
class Fruit {
public function __construct(public string $name = '') {}
}
$fruits = [
'orange' => new Fruit('Orange'),
'apple' => new Fruit('Apple'),
'pear' => new Fruit('Pear'),
'banana' => new Fruit('Banana'),
];
uksort($fruits, function ($a, $b) {
return strcasecmp($a, $b);
});
foreach (array_keys($fruits) as $key) {
print $key . ' ';
}
// apple banana orange pear
UASORT: Sorting an Array by Value, but Keeping the Keys in Place
The uasort method will sort the values of your array, keeping the keys associated to each value intact. Similar to usort and uksort, uasort accepts two arguments: the first being your array, and the second being your custom function which will perform the comparison between values.
class Fruit {
public function __construct(public string $name = '') {}
}
$fruits = [
'orange' => new Fruit('Orange'),
'apple' => new Fruit('Apple'),
'pear' => new Fruit('Pear'),
'banana' => new Fruit('Banana'),
];
uasort($fruits, function ($a, $b) {
return strcasecmp($a->name, $b->name);
});
foreach ($fruits as $key => $fruit) {
print $fruit->name . ' ';
}
// Apple Banana Orange Pear