note 120344 deleted from function.usort by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 16:46:12 +0000 |
| Subject: | note 120344 deleted from function.usort by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217844@lists.php.net to get a copy of this message | ||
Note Submitter: grzeniufication at gmail dot com
----
Just wanted to show off the beauty of PHPs spaceship operator in this use case.
<?php // tested on PHP 7.1
$a = [2, 1, 3, 6, 5, 4, 7];
$asc = $desc = $a;
usort($asc, function (int $a, int $b) { return ($a <=> $b); });
usort($desc, function (int $a, int $b) { return -($a <=> $b); });
print_r([ $a, $asc, $desc ]);
/**
* Getting ahead of myself but... If arrow function syntax was possible:
* usort($asc, (int $a, int $b) => ($a <=> $b));
* usort($desc, (int $a, int $b) => -($a <=> $b));
*/
?>