note 126669 deleted from function.usort by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 16:44:34 +0000 |
| Subject: | note 126669 deleted from function.usort by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217842@lists.php.net to get a copy of this message | ||
Note Submitter: Loenix
----
It seems the simplest way to compare integer is to subtract the second from the first
<?php
$values = array(6, 8, 1, 2, 3, 5, 7, 3, 8, 9, 2);
echo json_encode($values)."\n";// Original chaotic array
usort($values, function($a, $b) {
return $a - $b;
});
echo json_encode($values)."\n";// Values are sorted from smallest to greatest
?>
To sort from greatest to smallest, invert $a and $b:
<?php
usort($values, function($a, $b) {
return $b - $a;
});
?>
This code is compatible from PHP 5.3 to PHP 8.0