note 77412 added to function.ldap-sort
| From: | (T) | Date: | Tue, 28 Aug 2007 20:30:41 +0000 |
| Subject: | note 77412 added to function.ldap-sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129861@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 12.2.142.7
----
Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php
Edit -- https://master.php.net/note/edit/77412
Del: integrated -- https://master.php.net/note/delete/77412/integrated
Del: useless -- https://master.php.net/note/delete/77412/useless
Del: bad code -- https://master.php.net/note/delete/77412/bad+code
Del: spam -- https://master.php.net/note/delete/77412/spam
Del: non-english -- https://master.php.net/note/delete/77412/non-english
Del: in docs -- https://master.php.net/note/delete/77412/in+docs
Del: other reasons-- https://master.php.net/note/delete/77412
Reject -- https://master.php.net/note/reject/77412
Search -- https://master.php.net/manage/user-notes.php