PHPWales 2020 - June 3rd to June 4th


(PHP 4 >= 4.2.0, PHP 5, PHP 7)

ldap_sortSortiert LDAP Ergebniseinträge clientseitig


ldap_sort ( resource $link , resource $result , string $sortfilter ) : bool

Sortiere das Ergebnis einer LDAP-Suche, das von ldap_search() zurückgegeben wurde.

Da diese Funktion die zurückgegebenen Werte clientseitig sortiert, ist es möglich, dass unerwartete Ergebnisse geliefert werden, falls entweder das serverseitige sizelimit oder dasjenige, das mit ldap_search() festgelegt wurde, erreicht wurde.


Dieses Feature ist seit PHP 7.0.0 DEPRECATED (veraltet). Sich auf diese Funktion zu verlassen ist in keiner Weise empfehlenswert.



Eine LDAP-Verbindungskennung, geliefert von ldap_connect().


Eine Suchergebniskennung, geliefert von ldap_search().


Das als Sortierschlüssel zu verwendende Merkmal.


Sortieren eines Suchergebnisses.

Beispiel #1 LDAP Sortierung

// $ds ist eine gültige Verbindungskennung (siehe ldap_connect)

$dn        'ou=example,dc=org';
$filter    '(|(sn=Doe*)(givenname=John*))';
$justthese = array('ou''sn''givenname''mail');

$sr ldap_search($ds$dn$filter$justthese);

// Sortiere

// Hole die Daten
$info ldap_get_entries($ds$sr);

add a note add a note

User Contributed Notes 7 notes

zbaizman at yahoo dot com
15 years ago
This note may be self-evident, but the functionality of ldap_sort threw off this sometime user of relational databases.

The following code will NOT do what you expect:


// omitted calls to connect and and bind to LDAP server...

// attributes we want to retrieve from LDAP server
$ldap_attributes = array ( 'cn', 'o', 'mail' ) ;

// retrieve attributes from matching entries
$search_results = ldap_search ( $ldap_conn, 'dc=example,dc=org', '(objectclass=*)', 0, 500, 30 ) ;

// sort entries by last name ('sn')
ldap_sort ( $ldap_conn, $search_results, 'sn' ) ;


The entries will NOT be sorted by last name. Why not? Because LDAP doesn't function like a RDBMS; you cannot sort a result set on an arbitrary field, regardless of whether you "selected" it. You must always include the attribute by which you want to sort your entries among the requested attributes (add 'sn' to $ldap_attributes, in this case).

Hope this is helpful to some other folks who scratched their heads when they tried to sort entries and it didn't work out...
askgopal at sify dot com
16 years ago
Here's a simple LDAP sort function I wrote:

function sort_ldap_entries($e, $fld, $order)
    for (
$i = 0; $i < $e['count']; $i++) {
        for (
$j = $i; $j < $e['count']; $j++) {
$d = strcasecmp($e[$i][$fld][0], $e[$j][$fld][0]);
            switch (
$order) {
                if (
$d > 0)
swap($e, $i, $j);
                if (
$d < 0)
swap($e, $i, $j);
    return (

swap(&$ary, $i, $j)
$temp = $ary[$i];
$ary[$i] = $ary[$j];
$ary[$j] = $temp;

so that it can be invoked like:

= sort_ldap_entries($entries, 'mail', 'A'); // sort entries by ascending order of mail

    `$entries' is the array returned by ldap_get_entries() function.

This might be useful to those who still run older versions of PHP (<= 4.2.0) on their web servers :-)
matthew dot j dot gray at uwrf dot edu
17 years ago
This function applies strcmp() to each attribute (given by sortfilter) in order to sort the entries returned by the server.  To order search results ascending by last name, try passing "sn" as the sortfilter argument.  This function does not play nice with multi-valued attributes.
thorben at kapp-hamburg dot de
8 years ago
I wondered how to sort after a dn, for just show a tree view. I tried to set $sortfilter = 'dn', but that didn't work. Than I tried with a blank string ''. That worked, the entries are sorted by dn.
ben at xsusio dot com
15 years ago
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!)

* @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];
        do {
//create comparison variables from attributes:
$a = $b = null;
$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;
$is_greater = false;
        } while (
$j>0 && $is_greater);
$entries[$j] = $index;
jason dot sokolowski at rotork dot com
17 years ago
Something real simple i wrote to sort directory searches by a persons last name.


$lastname = $result[$i]["sn"][0];


//for i

fbernardi at bbros dot it
3 years ago
In order to order by multiple fields you can order your array many times:
For example:

        ldap_sort($link, $result, 'givenname');
        ldap_sort($link, $result, 'sn');
To Top