note 45377 added to function.ldap-sort
| From: | ben at xsusio dot com | Date: | Fri, 03 Sep 2004 21:33:33 +0000 |
| Subject: | note 45377 added to function.ldap-sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76000@lists.php.net to get a copy of this message | ||
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
/**
* @param array $entries
* @param array $attribs
* @desc Sort LDAP result entries by multiple attributes.
*/
function ldap_multi_sort(&$entries, $attribs){
for ($i=1; $i<$entries['count']; $i++){
$index = $entries[$i];
$j=$i;
do {
//create comparison variables from attributes:
$a = $b = null;
foreach($attribs as $attrib){
$a .= $entries[$j-1][$attrib][0];
$b .= $index[$attrib][0];
}
// do the comparison
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45377
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45377&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45377&report=yes
Search -- http://master.php.net/manage/user-notes.php