Sorting Query Results

Often applications need to present query results in a sorted order. There are several ways to achieve this with db4o.

This Pilot class will be used in the following examples:

Pilot.java
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.sorting; 04 05public class Pilot { 06 private String name; 07 08 private int points; 09 10 public Pilot(String name) { 11 this.name = name; 12 } 13 14 public Pilot(String name, int points) { 15 this.name = name; 16 this.points = points; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public int getPoints() { 24 return points; 25 } 26 27 public String toString() { 28 if (points == 0) { 29 return name; 30 } else { 31 return name + "/" + points; 32 } 33 } 34}

The database will be filled with the following Pilot objects:

SortingExample.java: setObjects
01public static void setObjects() { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME); 04 try { 05 for (int i = 0; i < 10; i++) { 06 for (int j = 0; j < 5; j++) { 07 Pilot pilot = new Pilot("Pilot #" + i, j + 1); 08 container.set(pilot); 09 } 10 } 11 } finally { 12 container.close(); 13 } 14 }

The following topics discuss some of the possible methods and point out their advantages and disadvantages.