note 56219 added to function.uksort
| From: | Commanaceatgmaildotcom at osu1 dot php dot net | Date: | Sat, 27 Aug 2005 16:38:41 +0000 |
| Subject: | note 56219 added to function.uksort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94264@lists.php.net to get a copy of this message | ||
I've written a small class that will sort arrays of objects by a certain attribute:
<?php
class t_object_sorter
{
protected $object_array;
protected $sort_by;
private function _comp($a,$b)
{
$key=$this->sort_by;
debug($this->object_array[$a],__FILE__,__LINE__);
if ($this->object_array[$a]->$key == $this->object_array[$b]->$key) return 0;
return ($this->object_array[$a]->$key < $this->object_array[$b]->$key) ? -1 : 1;
}
public function sort(&$object_array, $sort_by)
{
$this->object_array = $object_array;
$this->sort_by = $sort_by;
uksort($object_array, array($this, "_comp"));
}
}
?>
It is used like that:
<?php
$sorter = new t_object_sorter;
$sorter->sort($menu->item, 'position');
?>
This call will sort all "item"-objects of the object "menu" by their attribute
"position".
I hope this is helpfull.
----
Manual Page -- http://www.php.net/manual/en/function.uksort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56219
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56219&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56219&report=yes
Search -- http://master.php.net/manage/user-notes.php