note 88130 deleted from function.usort by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 08:11:38 +0000 |
| Subject: | note 88130 deleted from function.usort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212624@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
To sort reliably on more than one "column" in database-style arrays, handle this within
your cmp function where the first field to be sorted returns 0:
<?php
function cmp($a, $b){
if($a[field_1] == $b[field_1]){
if($a[field_2] == $b[field_2]){
return 0;
}
elseif($a[field_2] > $b[field_2]){
return -1;
}
elseif($a[field_2] < $b[field_2]){
return 1;
}
}
elseif($a[field_1] > $b[field_1]){
return -1;
}
elseif($a[field_1] < $b[field_1]){
return 1;
}
}
?>
Sorting by doing multiple passes does not seem to be reliable.