note 42264 added to function.uasort
| From: | david at rn2 dot php dot net | Date: | Mon, 10 May 2004 13:28:43 +0000 |
| Subject: | note 42264 added to function.uasort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69407@lists.php.net to get a copy of this message | ||
WARNING:-Regarding remmy.cjb.net (22-Oct-2003 05:57) mutisort() function:
Sorting by floating point numbers doesn't work in the current function. Use the modified
version below if you want to sort by a floating point column.
<?php
// Based on the other notes given before.
// Sorts an array (you know the kind) by key
// and by the comparison operator you prefer.
// Note that instead of most important criteron first, it's
// least important criterion first.
// The default sort order is ascending, and the default sort
// type is strnatcmp.
// function multisort($array[, $key, $order, $type]...)
function multisort($array)
{
for($i = 1; $i < func_num_args(); $i += 3)
{
$key = func_get_arg($i);
if (is_string($key)) $key = '"'.$key.'"';
$order = true;
if($i + 1 < func_num_args())
$order = func_get_arg($i + 1);
$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);
switch($type)
{
case 1: // Case insensitive natural.
$t = 'strcasecmp($a[' . $key . '], $b[' . $key . '])';
break;
case 2: // Numeric.
$t = '($a[' . $key . '] == $b[' . $key . ']) ? 0:(($a['
. $key . '] < $b[' . $key . ']) ? -1 : 1)';
break;
case 3: // Case sensitive string.
$t = 'strcmp($a[' . $key . '], $b[' . $key . '])';
break;
case 4: // Case insensitive string.
$t = 'strcasecmp($a[' . $key . '], $b[' . $key . '])';
break;
default: // Case sensitive natural.
$t = 'strnatcmp($a[' . $key . '], $b[' . $key . '])';
break;
}
echo $t;
usort($array, create_function('$a, $b', '; return ' . ($order ?
'' : '-') . '(' . $t . ');'));
}
return $array;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.uasort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42264
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42264&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42264&report=yes
Search -- http://master.php.net/manage/user-notes.php