不好意思。csdn来的少。
你如果使用的是netscape的ldap,且是4.0以上,就不需要上面说的那么麻烦了,有更好的API.给段sample给你,我在netscape LDAP5.0上运行通过。不过这段程序我也没有写完,有个TODO留在那里,不过不影响运行。
import java.util.Enumeration;
import java.util.Iterator;import netscape.ldap.*;
import netscape.ldap.controls.*;
/**
  *  Usage: Usage: java AsyncSortControl <host name> <login dn> <password> <searchBase>
 *
 */
public class AsyncSortControl {
    public static void main( String[] args ) {
        // Verify correct number of parameters
        if (args.length != 4) {
            System.out.println("Usage:   java AsyncSortControl <host name> "
            + "<login dn> <password> <container>");
            System.out.println("Example: java AsyncSortControl Acme.com"
            + " \"cn=admin,o=Acme\" secret \"ou=Sales,o=Acme\"");
            System.exit(0);
        }
       
        // Read command line arguments
        String  ldapHost    = args[0];
        String  loginDN     = args[1];
        String  password    = args[2];
        String  searchBase  = args[3];
        
                 
        int     MY_PORT = 389;
        
        try {
            // Create a LDAPConnection object
            LDAPConnection lc = new LDAPConnection();
            
            // Connect to server
            lc.connect( ldapHost, MY_PORT);
            lc.bind( loginDN, password );
            System.out.println( "Login succeeded");
            
            // We will be searching for all objects
            String MY_FILTER = "(objectClass=*)";
            
            //  Results of the search should include givenname and cn
            String[] attrs = new String[2];
            attrs[0] = "givenname";
            attrs[1] = "cn";
            
            // The results should be sorted using the cn attribute
            LDAPSortKey[] keys = new LDAPSortKey[1];
            keys[0] = new LDAPSortKey( "cn" );
            
            // Create a LDAPSortControl object - Fail if cannot sort
            LDAPSortControl sort = new LDAPSortControl( keys, true );
            
            // Set the Sort control to be sent as part of search request
            LDAPSearchConstraints cons = lc.getSearchConstraints();
            cons.setClientControls(sort);
            lc.setConstraints(cons);
            
            // Perform the search - ASYNCHRONOUS SEARCH USED HERE
            System.out.println( "Calling search request");
            
            LDAPSearchListener queue = lc.search( searchBase,LDAPConnection.SCOPE_SUB, MY_FILTER, attrs, false, (LDAPSearchListener) null, (LDAPSearchConstraints) null );
            
            LDAPMessage message;
            while (( message = queue.getResponse()) != null ) {
                
                // OPTION 1: the message is a search result reference
                if ( message instanceof LDAPSearchResultReference ) {
                    // Not following referrals to keep things simple
                    String urls[]=((LDAPSearchResultReference)message).getUrls();
                    System.out.println("Search result references:");
                    for ( int i = 0; i < urls.length; i++ )
                        System.out.println(urls[i]);
                }
                
                // OPTION 2:the message is a search result
                else if ( message instanceof LDAPSearchResult ) {
                    // Get the object name
                    LDAPEntry entry = ((LDAPSearchResult)message).getEntry();
                    
                    System.out.println("\n" + entry.getDN());
                    System.out.println("\tAttributes: ");
                    
                    // Get the attributes and print them out
                    LDAPAttributeSet attributeSet = entry.getAttributeSet();
                    Enumeration allAttributes = attributeSet.getAttributes();
                    
                    while(allAttributes.hasMoreElements()) {
                        LDAPAttribute attribute = (LDAPAttribute)allAttributes.nextElement();
                        String attributeName = attribute.getName();
                        
                        System.out.println("\t\t" + attributeName);
                        
                        // Print all values of the attribute
                        Enumeration allValues = attribute.getStringValues();
                        if( allValues != null) {
                            while(allValues.hasMoreElements()) {
                                String Value = (String) allValues.nextElement();
                                System.out.println("\t\t\t" + Value);
                            }
                        }
                    }
                }
                
                // OPTION 3: The message is a search response
                else {
                    LDAPResponse response = (LDAPResponse)message;
                    int status = response.getResultCode();
                    
                    // the return code is LDAP success
                    if ( status == LDAPException.SUCCESS ) {
                        System.out.println("Asynchronous search succeeded.");
                    }
                    
                    // the return code is referral exception
                    else if ( status == LDAPException.REFERRAL ) {
                        String urls[]=((LDAPResponse)message).getReferrals();
                        System.out.println("Referrals:");
                        for ( int i = 0; i < urls.length; i++ )
                            System.out.println(urls[i]);
                    }
                    else {
                        System.out.println("Asynchronous search failed.");
                        System.out.println( response.getErrorMessage());
                    }
                    
                    // Server should send back a control irrespective of the
                    // status of the search request
                    LDAPControl[] controls = response.getControls();
                    if ( controls != null ) {
                        
                        // Theoritically we could have multiple controls returned
                        for( int i = 0; i < controls.length; i++ ) {
                            
                            // We are looking for the LDAPSortResponse Control class - the control
                            // sent back in response to LDAPSortControl
                            //TODO:
                        }
                    }
                    
                }
            }
            
            // All done - disconnect
            if ( lc.isConnected() )
                lc.disconnect();
        }
        
        catch( LDAPException e ) {
            System.out.println( e.toString() );
        }
    }
}P.S:其实不用这些API也完全可以排序的,自己对结果集排序/《thinking in java》中也有提到,比如:
public int compare(Object c1 , Object c2) {
if (c1 == null && c2 != null) return -1;
if (c1 != null && c2 == null) return 1;
if (c1 == null && c2 == null ) return 0; String s1 = (String) c1;
String s2 = (String) c2;
return s1.compareTo(s2);
}
//sigh~~~~~