note 88130 added to function.usort
| From: | Anonymous at osu1 dot php dot net | Date: | Mon, 12 Jan 2009 11:14:39 +0000 |
| Subject: | note 88130 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149085@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 87.124.35.190
Probable Submitter: 82.71.7.179
----
Manual Page -- http://www.php.net/manual/en/function.usort.php
Edit -- https://master.php.net/note/edit/88130
Del: integrated -- https://master.php.net/note/delete/88130/integrated
Del: useless -- https://master.php.net/note/delete/88130/useless
Del: bad code -- https://master.php.net/note/delete/88130/bad+code
Del: spam -- https://master.php.net/note/delete/88130/spam
Del: non-english -- https://master.php.net/note/delete/88130/non-english
Del: in docs -- https://master.php.net/note/delete/88130/in+docs
Del: other reasons-- https://master.php.net/note/delete/88130
Reject -- https://master.php.net/note/reject/88130
Search -- https://master.php.net/manage/user-notes.php