Server Side Sorting

Request that the server sort results before returning them to the client. See RFC 2891. The SortKey class accepts three parameters that control the sorting:

SearchOperation search = new SearchOperation(new DefaultConnectionFactory("ldap://"));
SearchRequest request = SearchRequest.builder()
  .returnAttributes("cn", "sn")
  .controls(new SortRequestControl(new SortKey[] {new SortKey("sn", "caseExactMatch")}, true)) // sort by surname
SearchResponse response = search.execute(request);
for (LdapEntry entry : response.getEntries()) {
  // do something useful with the entry