Package edu.nrao.sss.sort

Sorting aides.

See:
          Description

Interface Summary
Orderable An object that has a sort order.
 

Class Summary
CompoundComparator<T> A comparator that is a chain of other comparators.
DoubleSortKey A sort key for Doubles (and doubles).
EnumSortKey<E extends Enum<E>> A sort key for enumerations.
IntSortKey A sort key for Integerss (and ints).
ReflectiveDoubleSortKey<T> A sort key and comparator that works with the Doubles returned by a method that is found reflectively.
ReflectiveEnumSortKey<T,E extends Enum<E>> A sort key and comparator that works with the enumeration elements returned by a method that is found reflectively.
ReflectiveIntSortKey<T> A sort key and comparator that works with the Integers returned by a method that is found reflectively.
ReflectiveStringSortKey<T> A sort key and comparator that works with the Strings returned by a method that is found reflectively.
SortKey<T> A single sorting key.
StringSortKey A sort key for Strings.
 

Enum Summary
SortOrder Gives direction to sort keys.
 

Package edu.nrao.sss.sort Description

Sorting aides.

The classes in this package support the sorting of collections, especially when there is a need to specify multiple sort keys for a single sort operation.

The easiest way to take advantage of this package is to use the simple CompoundComparator class. It allows clients to chain together multiple comparators, where succeeding comparators are enlisted in the sorting process only when preceding comparators could not distinguish between the objects being compared.

The remainder of the package may be broken down into two categories of classes: those that must be subclassed to be of use, and those that work via reflection and need not be subclassed. For both categories, the classes in those categories work as translators from the object being sorted to the actual property being compared. The general pattern is that a client is trying to sort objects of type T. This class of objects has properties P1, P2,...,Pn, each of which is of a (near-) primitive type. For each property, Pi, that should participate in the sorting property, the client provides either a subclass that extends PrimitiveTypeSortKey and implements Comparator<T>, or an object of type ReflectivePrimitiveTypeSortKey<PrimitiveType>, where PrimitiveType may (currently) be: String, Integer, Double, or Enum<E>.

Example:
A client wishes to sort a collection of class Person by gender, age, and first name, in that order. The Person class has the following methods:

One way to accomplish this sort would be as follows:
First create a subclass of StringSortKey in order to sort on first names. The code could look like this:

   public class PersonFirstNameKey extends StringSortKey
     implements Comparator<Person>
   {
     public int compare(Person p1, Person p2)
     {
       //Translate from Person to first name
       return compareObjects(p1.getName().get(0), p2.getName().get(0));
     }
   }
(Alternatively, one could create an anonymous class at the point of use.)
With that subclass in hand, the sorting code would look like this:
   ReflectiveEnumSortKey<Person,Gender> sexKey =
     new ReflectiveEnumSortKey<Person,Gender>("getGender");
     
   ReflectiveIntSortKey<Person> ageKey =
     new ReflectiveIntSortKey<Person>("getAge");
     
   PersonFirstNameKey<Person> nameKey =
     new PersonFirstNameKey<Person>;
     
   CompoundComparator<Person> comparator =
     new CompoundComparator<Person>(sexKey, ageKey, nameKey);
     
   Collections.sort(population, comparator);

Since:
2007-05-07
Author:
David M. Harland


Copyright © 2009. All Rights Reserved.