note 77412 deleted from function.ldap-sort by danbrown
| From: | danbrown@php.net | Date: | Wed, 31 Mar 2010 16:20:33 +0000 |
| Subject: | note 77412 deleted from function.ldap-sort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167570@lists.php.net to get a copy of this message | ||
Note Submitter: tarnishedniteNOSPA(T)MgMAIL
----
While bob's sort is nice I found that this one was a little more to my liking. This little
library ignores case and sorts on multiple criteria with some fairly short and simple code. I'm
sure it could be simplified further - switch statement perhaps.
<?php
/*Scoped global*/
$LDAPSortOrder;
/*$array is the return from ldap_get_entries
$sortBy is an array of values to sort by in the order in
which to sort them
$reverse will sort in reverse order if true
*/
function LDAPSort(&$array,$sortBy,$reverse=FALSE){
global $LDAPSortOrder;
$LDAPSortOrder = $sortBy;
if($reverse){
usort($array,'ldapcomparereverse');
} else {
usort($array,'ldapcompare');
}
}
/* The compare functions always check case insensitive*/
function ldapcompare($x,$y){
global $LDAPSortOrder;
/*
Loops through the items in order until a difference is found
*/
foreach($LDAPSortOrder as $ele){
if(strtolower($x[$ele][0]) == strtolower($y[$ele][0]))
continue;
elseif(strtolower($x[$ele][0]) < strtolower($y[$ele][0]))
return -1;
else
return 1;
}
return 0;
}
/*Just like above but in reverse order*/
function ldapcomparereverse($x,$y){
global $LDAPSortOrder;
foreach($LDAPSortOrder as $ele){
if(strtolower($x[$ele][0]) == strtolower($y[$ele][0]))
continue;
elseif(strtolower($x[$ele][0]) < strtolower($y[$ele][0]))
return 1;
else
return -1;
}
return 0;
}
?>
JML