SODA Sorting

SODA query API gives you a possibility to sort any field in ascending or descending order and combine sorting of different fields. For example, let's retrieve the objects of the Pilot class saved before, sorting "points" field in descending order and "name" field in ascending.

SortingExample.java: getObjectsSODA
01public static void getObjectsSODA(){ 02 03 ObjectContainer db = Db4o.openFile(YAPFILENAME); 04 try { 05 Query query = db.query(); 06 query.constrain(Pilot.class); 07 query.descend("name").orderAscending(); 08 query.descend("points").orderDescending(); 09 long t1 = System.currentTimeMillis(); 10 ObjectSet result= query.execute(); 11 long t2 = System.currentTimeMillis(); 12 long diff = t2 - t1; 13 System.out.println("Time to query and sort with SODA: " + diff + " ms."); 14 listResult(result); 15 } finally { 16 db.close(); 17 } 18 }


Obvious disadvantages of this method:

  • limitations of SODA queries (not type-safe and not compile-time checked);
  • limitations if sorting mechanism (only alphabetical for strings, numerical for numbers and object id for objects)

The valuable advantage of this method is its high performance.