note 26045 added to function.sort

From: Date: Wed, 16 Oct 2002 08:18:56 +0000
Subject: note 26045 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-38148@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.sort.php http://master.php.net/manage/user-notes.php?action=edit+26045 http://master.php.net/manage/user-notes.php?action=delete+26045 http://master.php.net/manage/user-notes.php?action=reject+26045

« previous php.notes (#38148) next »