note 94411 deleted from function.usort by danbrown
| From: | danbrown@php.net | Date: | Tue, 03 Nov 2009 15:49:57 +0000 |
| Subject: | note 94411 deleted from function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162359@lists.php.net to get a copy of this message | ||
Note Submitter: pau dot moreno at geemail
----
That's a port of Will Shaver's better implementation for to versions of PHP. Note that
string comparison is a bit poor though...
function osort(&$array, $props)
{
if(!is_array($props))
$props = array($props => true);
$funcBody = '';
foreach($props as $prop => $ascending)
{
$funcBody .= 'if($a->'.$prop.' != $b->'.$prop.'){';
if ($ascending)
$funcBody .= 'return $a->'.$prop.' > $b->'.$prop.'
? 1 : -1;';
else
$funcBody .= 'return $b->'.$prop.' > $a->'.$prop.'
? 1 : -1;';
$funcBody .= '}';
}
$funcBody .= 'return -1;';
usort($array, create_function ('$a, $b', $funcBody));
}