note 26045 deleted from function.sort by derick
| From: | derick@php.net | Date: | Wed, 31 Aug 2005 10:39:20 +0000 |
| Subject: | note 26045 deleted from function.sort by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94501@lists.php.net to get a copy of this message | ||
Note Submitter: fmmarzoa at gmx dot net
----
As someguys has note, there's a bug in the qsort_multiarray function. They've offered some
workarounds, but I think that the trouble its just that the algoritm is bad implemented, so
here's my solution:
function qsort_multiarray($array, $num = 0, $order = "ASC", $left = 0, $right = -1) {
if($right == -1) {
$right = count($array) - 1;
}
$links = $left;
$rechts = $right;
$mitte = $array[($left + $right) / 2][$num];
if($rechts > $links) {
do {
if($order == "ASC") {
while($array[$links][$num]<$mitte) $links++;
while($array[$rechts][$num]>$mitte) $rechts--;
} else {
while($array[$links][$num]>$mitte) $links++;
while($array[$rechts][$num]<$mitte) $rechts--;
}
if($links <= $rechts) {
$tmp = $array[$links];
$array[$links++] = $array[$rechts];
$array[$rechts--] = $tmp;
}
} while($links <= $rechts);
if ($left < $rechts) $array = qsort_multiarray($array,$num,$order,$left, $rechts);
if ($links < $right) $array = qsort_multiarray($array,$num,$order,$links,$right);
}
return $array;
}
Note the two "ifs" after the do {} while stub.