note 74034 deleted from function.ldap-sort by bobby
| From: | bobby@php.net | Date: | Wed, 21 Mar 2007 16:41:47 +0000 |
| Subject: | note 74034 deleted from function.ldap-sort by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-124678@lists.php.net to get a copy of this message | ||
Note Submitter: nick at lazy(hyphen)river dot net
----
Ben @ xsusio dot net's code below was very useful to me because although I'm not sure why,
ldap_sort against AD on the 'mail' attribute (or any other) seems to split the results
into 2 sections, so with a result set of around 230, I'd have about 180 (ish) email addresses
sorted alphabetically, but then another 50 (or so) records under those, but also sorted
alphabetically.
I never did work out why that happened (it wasn't to do with multiple values in the fields
though, or preceeding spaces in the email address) so Ben's solution meant I didn't have
to bother fixing it.
However, one drawback was that if a record didn't have an email address (not all our users do),
then Ben's sort would throw a warning (Notice: Undefined index: mail). This could also
adversely affect the sort so I added an if isset to deal with that instance.
Here's the modified code.
Thanks Ben.
<?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){
// only do it though if that attribute exists in both records
if(isset($entries[$j-1][$attrib]) && isset($index[$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;
}
?>