note 61305 added to function.sort
| From: | nm at thenoodleman dot com | Date: | Tue, 31 Jan 2006 01:18:09 +0000 |
| Subject: | note 61305 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102750@lists.php.net to get a copy of this message | ||
Faster, more effective function:
array_sort (array, ['asc'/'desc'])
Second parameter specifies whether to order ascending or descending. Default is ascending.
function array_sort($array, $type='asc'){
$result=array();
foreach($array as $var => $val){
$set=false;
foreach($result as $var2 => $val2){
if($set==false){
if($val>$val2 && $type=='desc' || $val<$val2 &&
$type=='asc'){
$temp=array();
foreach($result as $var3 => $val3){
if($var3==$var2) $set=true;
if($set){
$temp[$var3]=$val3;
unset($result[$var3]);
}
}
$result[$var]=$val;
foreach($temp as $var3 => $val3){
$result[$var3]=$val3;
}
}
}
}
if(!$set){
$result[$var]=$val;
}
}
return $result;
}
Works for ordering by integers or strings, no need to specify which.
Example:
$array=array('a' => 50, 'b' => 25, 'c' => 75);
print_r(array_sort($array));
Returns:
Array
(
[b] => 25
[a] => 50
[c] => 75
)
----
Server IP: 64.71.164.2
Probable Submitter: 71.224.50.127
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61305
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61305&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61305&report=yes
Search -- http://master.php.net/manage/user-notes.php