note 93260 added to function.usort

From: Date: Mon, 31 Aug 2009 10:58:04 +0000
Subject: note 93260 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-160077@lists.php.net to get a copy of this message
<?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); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 77.42.146.157 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/93260 Del: integrated -- https://master.php.net/note/delete/93260/integrated Del: useless -- https://master.php.net/note/delete/93260/useless Del: bad code -- https://master.php.net/note/delete/93260/bad+code Del: spam -- https://master.php.net/note/delete/93260/spam Del: non-english -- https://master.php.net/note/delete/93260/non-english Del: in docs -- https://master.php.net/note/delete/93260/in+docs Del: other reasons-- https://master.php.net/note/delete/93260 Reject -- https://master.php.net/note/reject/93260 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160077) next »