note 87032 deleted from function.sort by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 08:36:37 +0000 |
| Subject: | note 87032 deleted from function.sort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212791@lists.php.net to get a copy of this message | ||
Note Submitter: jalil at measat dot org
----
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.
<?php
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;
}
}
?>