If you are wanting to sort by multiple attributes, for instance ordering by last name and then first name, try this function. This is similar to "ORDER BY lastname, firstname" in SQL.
This function uses an insertion sort algorithm, which is somewhat faster then the old-fashoned bubble sort. The second argument is an array containing the attributes you want to sort by. (this functon won't do descending or ascending.. feel free to add it!)
<?php
function ldap_multi_sort(&$entries, $attribs){
for ($i=1; $i<$entries['count']; $i++){
$index = $entries[$i];
$j=$i;
do {
$a = $b = null;
foreach($attribs as $attrib){
$a .= $entries[$j-1][$attrib][0];
$b .= $index[$attrib][0];
}
if ($a > $b){
$is_greater = true;
$entries[$j] = $entries[$j-1];
$j = $j-1;
}else{
$is_greater = false;
}
} while ($j>0 && $is_greater);
$entries[$j] = $index;
}
return $entries;
}
?>