note 93260 deleted from function.usort by danbrown
| From: | danbrown@php.net | Date: | Mon, 31 Aug 2009 18:58:35 +0000 |
| Subject: | note 93260 deleted from function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160091@lists.php.net to get a copy of this message | ||
Note Submitter: roger dot tannous at gmail dot com
----
<?php
// Note: discard the values in the date field.
$_logs[] = array
(
"operator" => 'OP1',
"size" => 200,
"date" => mktime(0, 0, 0, date("m"), date("d") - 20,
date("Y"))
);
$_logs[] = array
(
"operator" => 'OP2',
"size" => 400,
"date" => mktime(0, 0, 0, date("m"), date("d") - 10,
date("Y"))
);
$_logs[] = array
(
"operator" => 'OP3',
"size" => 100,
"date" => mktime(0, 0, 0, date("m"), date("d") - 4,
date("Y"))
);
$_logs[] = array
(
"operator" => 'OP3',
"size" => 200,
"date" => mktime(0, 0, 0, date("m"), date("d") - 50,
date("Y"))
);
$_logs[] = array
(
"operator" => 'OP1',
"size" => 800,
"date" => mktime(0, 0, 0, date("m"), date("d") - 10,
date("Y"))
);
echo "Before sorting:: ";
print_r($_logs);
echo "<br/><br/>";
// Let's reverse the array
/*
The following 3 fields can be 'filled' programmatically,
so you can request sorting on as much as there are fields
in the array on which sorting is required.
Note: Choose the right ($CMP_FIELD, $CMD_TYPE) pairs !
*/
// In this example: 'operator'-ASCending, then 'size'-DESCending
$CMP_ASCENDING = array(true, false);
$CMP_FIELDS = array("operator", "size");
$CMP_TYPES = array("textual", "numeric"); // numeric / textual
function cmp_numeric($a, $b)
{
if ($a == $b)
return 0;
return ($a > $b?1:-1);
}
function cmp_textual($a, $b)
{
return strcmp($a, $b);
}
/*
function cmp_new_compare_function($a, $b)
{
return 0;
}
*/
function cmp($a, $b, $arrayIndex = 0)
{
// here, $a and $b are arrays and not simple variables
global $CMP_ASCENDING, $CMP_FIELDS, $CMP_TYPES;
$cmpResult = 0; // initially, assume they are equal
switch($CMP_TYPES[$arrayIndex])
{
case "numeric":
$cmpResult = cmp_numeric($a[$CMP_FIELDS[$arrayIndex]],
$b[$CMP_FIELDS[$arrayIndex]]);
break;
case "textual":
$cmpResult = cmp_textual($a[$CMP_FIELDS[$arrayIndex]],
$b[$CMP_FIELDS[$arrayIndex]]);
break;
/*
case "new_compare_type_here":
$cmpResult = cmp_new_compare_function($a[$CMP_FIELDS[$arrayIndex]],
$b[$CMP_FIELDS[$arrayIndex]]);
break;
*/
default:
// do nothing here !
break;
}
if ($cmpResult != 0)
{
$cmpResult = ($cmpResult > 0?($CMP_ASCENDING[$arrayIndex]?1:-1)
:($CMP_ASCENDING[$arrayIndex]?-1:1));
}
else if ($arrayIndex < (count($CMP_FIELDS) - 1))
{
$cmpResult = cmp($a, $b, ++$arrayIndex);
}
return $cmpResult;
}
usort($_logs, "cmp");
echo "<br/><br/>Sorting:: 'operator'-ASCending, then
'size'-DESCending:: ";
print_r($_logs);
echo "<br/><br/>Sorting:: 'size'-DESC, then 'operator'-ASC::
";
$CMP_ASCENDING = array(false, true);
$CMP_FIELDS = array("size", "operator");
$CMP_TYPES = array("numeric", "textual");
usort($_logs, "cmp");
print_r($_logs);
echo "<br/><br/>Sorting:: 'size'-DESC, then 'operator'-DESC::
";
$CMP_ASCENDING = array(false, false);
$CMP_FIELDS = array("size", "operator");
$CMP_TYPES = array("numeric", "textual");
usort($_logs, "cmp");
print_r($_logs);
?>