[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: sorting results in JLDAP

Jon Roberts wrote:
I noticed the deprecation of the sort() method in the LDAPSearchResults class this morning. Are there any examples of sorting search results using the Comparable interface?

I got a response from Novell offline (thanks!), but let me complete this thread with an answer to this question so that anybody else can clear up the same misconception by searching the archives.

My understanding is that if I get some search results and would like to sort them client side, I would first need to convert them to a collection of LDAPEntry instances.

This much is true.

If I want to sort by a given attribute value, it appears I would have to subclass LDAPEntry and override the compareTo() method with my own class/routine that extracts and compares the attribute values.

This is not. Using the class com.novell.ldap.LDAPCompareAttrNames and the static java.util.Collections.sort() method, I can do something like this with say an ArrayList of entries:

Collections.sort( mylist, new LDAPCompareAttrNames("myattribute") );

to reorder the entries by locale-sensitive string comparison of the values for myattribute. Other constructors and methods for the LDAPCompareAttrNames class support using alternate locales, sorting by multiple attributes, and reversing the order of a given sort.

It would be great to expand this with support for other syntaxes, but I'm more than happy to have found this solution, especially given that such a requirement would've been inconceivably prohibitive under the old LDAPSearchResults.sort() way. If anything, this points to a need for more documentation and examples. When I'm done with my project, I hope to have plenty to share.

Thanks for JLDAP.

Jon Roberts