note 87032 added to function.sort
| From: | jalil at measat dot org | Date: | Sat, 15 Nov 2008 21:48:41 +0000 |
| Subject: | note 87032 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146585@lists.php.net to get a copy of this message | ||
this is an implementation of the complement of
Matthew Hood's objectSort (http://my.php.net/manual/en/function.sort.php#75036), which i found
very convenient for sorting objects.
this does the reverse, it sorts according to the key
selected for the object but in reverse order.
and having both sort methods allows consistency and convenience for sorting objects, if speed
isn't your major concern.
the only change ( apart form data being reworded as object ) is the use of < instead of > in
the original.
you could of couse incorporate all in one routine, but why
complicate matters.
function objectRSort(&$object, $key)
{
for ($i = count($object) - 1; $i >= 0; $i--)
{
$swapped = false;
for ($j = 0; $j < $i; $j++)
{
if ($object[$j]->$key < $object[$j + 1]->$key)
{
$tmp = $object[$j];
$object[$j] = $object[$j + 1];
$object[$j + 1] = $tmp;
$swapped = true;
}
}
if (!$swapped) return;
}
}
----
Server IP: 124.217.231.13
Probable Submitter: 60.50.203.100
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/87032
Del: integrated -- https://master.php.net/note/delete/87032/integrated
Del: useless -- https://master.php.net/note/delete/87032/useless
Del: bad code -- https://master.php.net/note/delete/87032/bad+code
Del: spam -- https://master.php.net/note/delete/87032/spam
Del: non-english -- https://master.php.net/note/delete/87032/non-english
Del: in docs -- https://master.php.net/note/delete/87032/in+docs
Del: other reasons-- https://master.php.net/note/delete/87032
Reject -- https://master.php.net/note/reject/87032
Search -- https://master.php.net/manage/user-notes.php