note 100279 added to function.uasort
| From: | phire_sk at osu1 dot php dot net | Date: | Tue, 05 Oct 2010 21:19:46 +0000 |
| Subject: | note 100279 added to function.uasort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172986@lists.php.net to get a copy of this message | ||
I tried using some of the previous built multisorts (specifically the one by Alt_F4 on 10-Oct-2009
03:34), but they weren't working as expected.
So, I made my own Class, and it seems to work wonderfully.
Here is the code:
<?php
/************************************
* Allows sorting multi-dimensional
* arrays by a specific key and in
* asc or desc order
**/
class multiSort
{
var $key; //key in your array
//runs the sort, and returns sorted array
function run ($myarray, $key_to_sort, $type_of_sort = '')
{
$this->key = $key_to_sort;
if ($type_of_sort == 'desc')
uasort($myarray, array($this, 'myreverse_compare'));
else
uasort($myarray, array($this, 'mycompare'));
return $myarray;
}
//for ascending order
function mycompare($x, $y)
{
if ( $x[$this->key] == $y[$this->key] )
return 0;
else if ( $x[$this->key] < $y[$this->key] )
return -1;
else
return 1;
}
//for descending order
function myreverse_compare($x, $y)
{
if ( $x[$this->key] == $y[$this->key] )
return 0;
else if ( $x[$this->key] > $y[$this->key] )
return -1;
else
return 1;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 173.10.36.174
----
Manual Page -- http://www.php.net/manual/en/function.uasort.php
Edit -- https://master.php.net/note/edit/100279
Del: integrated -- https://master.php.net/note/delete/100279/integrated
Del: useless -- https://master.php.net/note/delete/100279/useless
Del: bad code -- https://master.php.net/note/delete/100279/bad+code
Del: spam -- https://master.php.net/note/delete/100279/spam
Del: non-english -- https://master.php.net/note/delete/100279/non-english
Del: in docs -- https://master.php.net/note/delete/100279/in+docs
Del: other reasons-- https://master.php.net/note/delete/100279
Reject -- https://master.php.net/note/reject/100279
Search -- https://master.php.net/manage/user-notes.php