Custom Sorter

Home > Object Persistency API > API Reference > Custom Sorter

Custom sorters are implemented in Java. A sorter is a Java class which can sort entities of a specific class extent. Please note that sorters are not used in the context of database queries – due to performance reasons the query language offers an order by feature. But they can be applied when navigating from an entity object to a set of related entities.

A sorter class must implement the java.util.Comparator<T> interface and therefore provides an implementation for the following method:

public int compare(T o1, T o2);

This method has to decide whether the entity o1 is less than, equal or greater than the entity o2 by returning -1, 0 or +1 respectively.

We recommend wrapping all sorter implementations which can sort entities of a specific class extent in a class which is named just like the corresponding entity class plus the suffix Sorter. Moreover this wrapper class should be stored within the same package as the entity class. The actual sorter classes should be static, so that you can reach them comfortably in your applications.

Let us give you an example again:

package org.oomega.base;

import java.util.Comparator;

public class EntityObjectSorter {

    public static class ByOid implements Comparator<EntityObject> {
        public int compare(EntityObject eo1, EntityObject eo2) {
            return eo1.getOid().compareTo(eo2.getOid());
        }
    }

    public static class ByCid implements Comparator<EntityObject> {
        public int compare(EntityObject eo1, EntityObject eo2) {
            int result;
            result = eo1.getCid().compareTo(eo2.getCid());
            if (result == 0) {
                result = eo1.getOid().compareTo(eo2.getOid());
            }
            return result;
        }
    }
}

The sorter can sort entities of type EntityObject, hence the wrapper class is named EntityObjectSorter which is placed in the package org.oomega.base just like the entity class EntityObject itself. The sorter classes ByOid and ByCid are static and implement the Comparator<EntityObject> interface, consequently the two parameters of the compare method are of type EntityObject.

Next chapter: jSDL

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.