Download the JDK
Search the Tutorials
Hide the TOC
Trail: Java Naming and Directory Interface(TM).
Lesson: New features in JDK 5.0 and JDK 6
New features in JDK 5.0 and JDK 6
Retrieving Distinguished Name
Standard LDAP Controls
Paged Results Control
Sort Control
Manage Referral Control
Manipulating LdapName (Distinguished Name)
Manipulating Relative Distringuished Name (RDN)
Setting Timeout for Ldap Operations
Home Page > Java Naming and Directory Interface(TM). > New features in JDK 5.0 and JDK 6
« Previous • Trail • Next »
Sort Control

> The sort control is used when a client wants the server to send the sorted search results. The server-side sorting is useful in a situation where the client needs to sort the results according to some criteria but is not equipped to perform the sort process on its own. The sort control is specified in RFC 2891. The classes below provide the functionality required to support sort control.
> - `javax.naming.ldap.SortControl`
> - `javax.naming.ldap.SortKey`
> - `javax.naming.ldap.SortResponseControl`

> The SortKey is an odered list of keys based upon which the server sorts the result.

> ##### How to use Sort Control?

> The example below illustrates the client-server interaction between a client performing a search requesting a server-side sorting based on the attribute "cn".
> 1. Client sends a search request asking for

>            // Activate sorting
>             String sortKey = "cn";
>             ctx.setRequestControls(new Control[] {
>                 new SortControl(sortKey, Control.CRITICAL) });

>             // Perform a search
>             NamingEnumeration results =
>                 ctx.search("", "(objectclass=*)", new SearchControls());

> 2. The server responds with entries that are sorted based on the "cn" attribute and its corresponding matching rule.

>            // Iterate over sorted search results
>             while (results != null && results.hasMore()) {
>                 // Display an entry
>                 SearchResult entry = (SearchResult)results.next();
>                 System.out.println(entry.getName());

>                 // Handle the entry's response controls (if any)
>                 if (entry instanceof HasControls) {
>                     // ((HasControls)entry).getControls();
>                 }
>             }
>             // Examine the sort control response
>             Control[] controls = ctx.getResponseControls();
>             if (controls != null) {
>                 for (int i = 0; i < controls.length; i++) {
>                     if (controls[i] instanceof SortResponseControl) {
>                         SortResponseControl src = (SortResponseControl)controls[i];
>                         if (! src.isSorted()) {
>                             throw src.getException();
>                         }
>                     } else {
>                         // Handle other response controls (if any)
>                     }
>                 }
>             }

>    The complete JNDI example can be found here

>    Note: The sort control is supported by both Sun Java Directory Server and the Windows Active Directory server.

« Previous • Trail • Next »
Previous page: Paged Results Control
Next page: Manage Referral Control