note 42264 deleted from function.uasort by cmb
| From: | cmb@php.net | Date: | Wed, 22 Jun 2016 17:27:19 +0000 |
| Subject: | note 42264 deleted from function.uasort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205786@lists.php.net to get a copy of this message | ||
Note Submitter: david [__at__] castlelaing.com
----
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;
}
?>