Introduce some associative array sorting for ldap

This commit is contained in:
Miles Lott 2001-03-22 19:48:14 +00:00
parent 78a1c16d32
commit dbe10c6a6d

View File

@ -71,56 +71,87 @@
} }
} }
// sort a multi-dimensional array on an array element function asc_sort($a,$b) {
// using the named element echo "<br>A:'".$a."' B:'".$b;
// This is neither used (yet) or guaranteed to work if($a[1]==$b[1]) return 0;
function qsort_multiarray($array,$column,$order = "ASC",$left = 0,$right = -1,$num=0) return ($a[1]>$b[1])?1:-1;
{ }
if($right == -1)
{ $right = count($array) - 1; }
$i=0; function desc_sort($a,$b) {
echo "<br>A:'".$a."' B:'".$b;
if (!$num) { if($a[1]==$b[1]) return 0;
$num=0; return ($a[1]<$b[1])?1:-1;
echo "nonum"; }
if ($column && !empty($column)) {
while (list($name,$value) = each($array[0])) { /**
if ($column == $name) { ** comesafter ($s1, $s2)
$num = $i; **
break; ** Returns 1 if $s1 comes after $s2 alphabetically, 0 if not.
} **/
$i++; function comesafter ($s1, $s2) {
/**
** We don't want to overstep the bounds of one of the strings and segfault,
** so let's see which one is shorter.
**/
$order = 1;
if (strlen ($s1) > strlen ($s2)) {
$temp = $s1;
$s1 = $s2;
$s2 = $temp;
$order = 0;
}
for ($index = 0; $index < strlen ($s1); $index++) {
/* $s1 comes after $s2 */
if (strtolower($s1[$index]) > strtolower($s2[$index])) { return ($order); }
/* $s1 comes before $s2 */
if (strtolower($s1[$index]) < strtolower($s2[$index])) { return (1 - $order); }
}
/* Special case in which $s1 is a substring of $s2 */
return ($order);
}
/*
* asortbyindex ($sortarray, $index)
*
* Sort a multi-dimensional array by a second-degree index. For instance, the 0th index
* of the Ith member of both the group and user arrays is a string identifier. In the
* case of a user array this is the username; with the group array it is the group name.
* asortby
*/
function asortbyindex ($sortarray, $index) {
$lastindex = count ($sortarray) - 1;
for ($subindex = 0; $subindex < $lastindex; $subindex++) {
$lastiteration = $lastindex - $subindex;
for ($iteration = 0; $iteration < $lastiteration; $iteration++) {
$nextchar = 0;
if ($this->comesafter($sortarray[$iteration][$index], $sortarray[$iteration + 1][$index])) {
$temp = $sortarray[$iteration];
$sortarray[$iteration] = $sortarray[$iteration + 1];
$sortarray[$iteration + 1] = $temp;
} }
} }
} }
echo "<br>". $num ." - name='".$name."', value='". $value . "'"; return ($sortarray);
}
$lefts = $left; function arsortbyindex ($sortarray, $index) {
$rights = $right; $lastindex = count ($sortarray) - 1;
$middle = $array[($left + $right) / 2][$num]; for ($subindex = $lastindex; $subindex > 0; $subindex--) {
$lastiteration = $lastindex - $subindex;
if($rights > $lefts) { for ($iteration = $lastiteration; $iteration > 0; $iteration--) {
do { $nextchar = 0;
if($order == "ASC") { if ($this->comesafter($sortarray[$iteration][$index], $sortarray[$iteration - 1][$index])) {
while($array[$lefts][$num]<$middle) $lefts++; $temp = $sortarray[$iteration];
while($array[$rights][$num]>$middle) $rights--; $sortarray[$iteration] = $sortarray[$iteration - 1];
} else { $sortarray[$iteration - 1] = $temp;
while($array[$lefts][$num]>$middle) $lefts++;
while($array[$rights][$num]<$middle) $rights--;
} }
}
if($lefts <= $rights) { }
$tmp = $array[$lefts]; return ($sortarray);
$array[$lefts++] = $array[$rights];
$array[$rights--] = $tmp;
}
} while($lefts <= $rights);
$array = $this->qsort_multiarray($array,"",$order,$left,$rights,$num);
$array = $this->qsort_multiarray($array,"",$order,$lefts,$right,$num);
}
return $array;
} }
} }
?> ?>