note 126669 added to function.usort

From: Date: Wed, 08 Dec 2021 16:28:14 +0000
Subject: note 126669 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-217340@lists.php.net to get a copy of this message
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 ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 92.167.42.238) ---- Manual Page -- https://php.net/manual/en/function.usort.php Edit -- https://main.php.net/note/edit/126669 Del: integrated -- https://main.php.net/note/delete/126669/integrated Del: useless -- https://main.php.net/note/delete/126669/useless Del: bad code -- https://main.php.net/note/delete/126669/bad+code Del: spam -- https://main.php.net/note/delete/126669/spam Del: non-english -- https://main.php.net/note/delete/126669/non-english Del: in docs -- https://main.php.net/note/delete/126669/in+docs Del: other reasons-- https://main.php.net/note/delete/126669 Reject -- https://main.php.net/note/reject/126669 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217340) next »