note 120344 added to function.usort
| From: | grzeniufication at gmail dot com | Date: | Fri, 16 Dec 2016 19:56:25 +0000 |
| Subject: | note 120344 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-208608@lists.php.net to get a copy of this message | ||
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));
*/
?>
----
Server IP: 72.52.91.14
Probable Submitter: 83.8.133.82
----
Manual Page -- http://php.net/manual/en/function.usort.php
Edit -- https://master.php.net/note/edit/120344
Del: integrated -- https://master.php.net/note/delete/120344/integrated
Del: useless -- https://master.php.net/note/delete/120344/useless
Del: bad code -- https://master.php.net/note/delete/120344/bad+code
Del: spam -- https://master.php.net/note/delete/120344/spam
Del: non-english -- https://master.php.net/note/delete/120344/non-english
Del: in docs -- https://master.php.net/note/delete/120344/in+docs
Del: other reasons-- https://master.php.net/note/delete/120344
Reject -- https://master.php.net/note/reject/120344
Search -- https://master.php.net/manage/user-notes.php