org.kuali.kfs.sys
Class DynamicCollectionComparator<T>

java.lang.Object
  extended by org.kuali.kfs.sys.DynamicCollectionComparator<T>
All Implemented Interfaces:
Serializable, Comparator<T>

public class DynamicCollectionComparator<T>
extends Object
implements Comparator<T>, Serializable

The comparator can dynamically implement java.util.Comparator and facilitate to sort a given colletion. This implementation is based on an article by York Davis, which was published in Java Developer's Journal (http://java.sys-con.com/read/45837.htm).

See Also:
Serialized Form

Nested Class Summary
static class DynamicCollectionComparator.SortOrder
          enumerate the valid values of sort order
 
Method Summary
 int compare(T object0, T object1)
          compare the two given objects for order.
 int compare(T object0, T object1, String fieldName)
          compare the two given objects for order.
 int getSortOrderAsNumber()
          convert the sort order as an interger.
static
<C> void
sort(List<C> list, DynamicCollectionComparator.SortOrder sortOrder, String... fieldNames)
          sort the given collection ordered by the given field name
static
<C> void
sort(List<C> list, String... fieldNames)
          sort the given collection ordered by the given field name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Method Detail

sort

public static <C> void sort(List<C> list,
                            String... fieldNames)
sort the given collection ordered by the given field name. Ascending order is used.

Parameters:
list - the given collection that needs to be sorted
fieldName - the field name ordered by

sort

public static <C> void sort(List<C> list,
                            DynamicCollectionComparator.SortOrder sortOrder,
                            String... fieldNames)
sort the given collection ordered by the given field name

Parameters:
list - the given collection that needs to be sorted
fieldName - the field name ordered by
sortOrder - the given sort order, either ascending or descending

compare

public int compare(T object0,
                   T object1)
compare the two given objects for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. If the objects implement Comparable interface, the objects compare with each other based on the implementation; otherwise, the objects will be converted into Strings and compared as String.

Specified by:
compare in interface Comparator<T>

compare

public int compare(T object0,
                   T object1,
                   String fieldName)
compare the two given objects for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. If the objects implement Comparable interface, the objects compare with each other based on the implementation; otherwise, the objects will be converted into Strings and compared as String.


getSortOrderAsNumber

public int getSortOrderAsNumber()
convert the sort order as an interger. If the sort order is "DESC" (descending order), reutrn -1; otherwise, return 1.

Returns:
-1 if the sort order is "DESC" (descending order); otherwise, return 1.


Copyright © 2005-2012 The Kuali Foundation. All Rights Reserved.