note 44163 deleted from function.array-multisort by aidan
| From: | aidan@php.net | Date: | Sun, 03 Oct 2004 10:38:38 +0000 |
| Subject: | note 44163 deleted from function.array-multisort by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77789@lists.php.net to get a copy of this message | ||
Note Submitter: artox at online dot no
----
I made this little function for sorting small multi-dim arrays by a chosen column in multiple rows.
Sorts numeric, ascending.
<?php
// $arr = array to sort.
// $col = column to sort by.
function incision_sort($arr, $col){
for($k = 0; $k < sizeof($arr)-1; $k++){
// $arr[$k+1] is possibly in the wrong place. Take it out.
$t = $arr[$k+1];
$i = $k;
// Push $arr[i] to the right until we find the right place for $t.
while($i >= 0 && $arr[$i][$col] > $t[$col]){
$arr[$i+1] = $arr[$i];
$i--;
}
// Insert $t into the right place.
$arr[$i+1] = $t;
}// End sort
return $arr;
}
?>