note 54901 deleted from function.usort by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 16:34:19 +0000 |
| Subject: | note 54901 deleted from function.usort by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217835@lists.php.net to get a copy of this message | ||
Note Submitter: dmhouse at gmail dot com
----
The easiest way to compare two integers is just to take the second away from the first. For example,
say you wanted to sort by an integer property of an object. Your comparison function would look like
this:
<?php
function compare_counts($a, $b) {
return $a->count - $b->count;
}
?>
This works because you don't necessarily have to return -1, 0 or 1, the manual states any
integer less than, equal to or greater than 0 is OK.