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

The database will be filled with the following Pilot objects:

SortingExample.java: setObjects
01public static void setObjects(){ 02 new File(YAPFILENAME).delete(); 03 ObjectContainer db = Db4o.openFile(YAPFILENAME); 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 db.set(pilot); 09 } 10 } 11 } finally { 12 db.close(); 13 } 14 }

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