note 49940 added to function.uasort

From: Date: Sun, 13 Feb 2005 02:09:19 +0000
Subject: note 49940 added to function.uasort
Groups: php.notes 
Request: Send a blank email to php-notes+get-84909@lists.php.net to get a copy of this message
The following is a modification of the dholme/messju masort func using david's float code with automatic data type detection. As long as the value isn't a string numeral (I.E. "1", '13.4') this should sort strings and numbers without having to explicity set which they are. <?php function masort(&$data, $sortby) { static $sort_funcs = array(); if (empty($sort_funcs[$sortby])) { $code = "\$c=0;"; foreach (split(',', $sortby) as $key) { $array = array_pop($data); array_push($data, $array); if(is_numeric($array[$key])) $code .= "if ( \$c = ((\$a['$key'] == \$b['$key']) ? 0:((\$a['$key'] < \$b['$key']) ? -1 : 1 )) );"; else $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return \$c;\n"; } $code .= 'return $c;'; $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code); } else { $sort_func = $sort_funcs[$sortby]; } $sort_func = $sort_funcs[$sortby]; uasort($data, $sort_func); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.uasort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49940 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49940&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49940&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49940&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49940&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49940&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49940&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84909) next »