In some cases you may find it necessary to use Evaluations. There is no standard sorting API for the Evaluation results. But you can sort the returned result set using standard Java collection API.
For example, let's retrieve the objects of the Pilot class saved before, selecting only pilots with even points and sorting them according to their name:
01public static void getObjectsEval() { 02
03
ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME); 04
try { 05
long t1 = System.currentTimeMillis(); 06
Query query = container.query(); 07
query.constrain(Pilot.class); 08
query.constrain(new Evaluation() { 09
public void evaluate(Candidate candidate) { 10
Pilot pilot = (Pilot) candidate.getObject(); 11
candidate.include(pilot.getPoints() % 2 == 0); 12
} 13
}); 14
List<Pilot> result = new ArrayList<Pilot>(query.execute()); 15
Collections.sort(result, new Comparator<Pilot>() { 16
public int compare(Pilot p1, Pilot p2) { 17
return p1.getName().compareTo(p2.getName()); 18
} 19
}); 20
long t2 = System.currentTimeMillis(); 21
long diff = t2 - t1; 22
System.out 23
.println("Time to execute with Evaluation query and collection sorting: " 24
+ diff + " ms."); 25
listResult(result); 26
} finally { 27
container.close(); 28
} 29
}
This sorting method can be used to sort query results when the sorting can not be added to the query (Evaluations, QBE).