I am using the Apache Directory API to query an OpenLDAP server. I think this code should return the results sorted by sn but they are returned in random order.
SearchRequest req = new SearchRequestImpl();
req.setScope(SearchScope.ONELEVEL);
req.addAttributes("*");
req.setTimeLimit(0);
req.setBase(searchDn);
req.setFilter("(objectclass=posixAccount)");
SortRequest sortRequest = new SortRequestControlImpl();
sortRequest.addSortKey(new SortKey("sn"));
req.addControl(sortRequest);
try (EntryCursor cursor = new EntryCursorImpl(connection.search(req))) {
for (Entry entry : cursor) {
System.out.println("sn: " + entry.get("sn").getString());
}
}
The output of this code from the data in my test LDAP Server is:
sn: Trainee
sn: Admin
sn: User
sn: Supervisor
sn: Supervisor
I based the code on this Integration test https://github.com/apache/directory-server/blob/master/server-integ/src/test/java/org/apache/directory/server/operations/search/SortedSearchIT.java and I can't see what I have done wrong.
Can anyone offer any advice? Thanks.
sk.setReverseOrder( true );on the SortKey, so I think the sorting is just being ignored.try (SearchCursor cursor = connection.search(req)) { SearchResultEntry se = (SearchResultEntry) cursor.get(); Entry entry = se.getEntry(); System.out.println("sn: " + entry.get("sn").getString()); }