Package snap.util
Class Sort
java.lang.Object
snap.util.Sort
- All Implemented Interfaces:
Cloneable
,Comparator
,JSArchiver.GetKeys
This class provides a basic description for comparison and sorting with a simple key and order (ascending or
descending). You can create a new order like this:
Sort titleFirst = new Sort("Title"); Sort bestRevenueFirst = new Sort("Revenue", ORDER_DESCEND);
This class also provides useful static methods for comparison and sorting:
Sort.sort(myList, "Title"); // Sort myList by its contents' getTitle method (alphabetically) Sort.sort(myList, bestRevenueFirst); // Sort myList by its contents' getRevenue method (largest first) List mySortList = new ArrayList(); mySortList.add(bestRevenueFirst); mySortList.add(titleFirst); Sort.sort(myList, mySortList); // Sort myList by revenue and title
-
Field Summary
Modifier and TypeFieldDescriptionstatic Comparator
static final byte
static final byte
static final byte
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionclone()
Standard clone implementation.int
Returns how the two given objects compare with this sort.static int
Compare two value objects (assumed to be String, Number, Boolean, Date, Comparable, etc.).boolean
Standard equals implementation.RMJSONArchiver GetKeys method.getKey()
Returns the key for this sort.int
getOrder()
Returns the order for this sort.void
Sets the key for this sort.void
setOrder
(int anOrder) Sets the order for this sort.static void
Returns the given list sorted by the given key.static void
Returns the given list sorted by the given key.static void
Returns the given list sorted by the given list of sorts.static void
Returns the given list sorted by the given sort.static void
Returns the given list sorted by the given sort.static <T extends Comparable<? super T>>
List<T>sortedList
(Collection<T> aCollection) Returns a new sorted list from given collection.static <T> List<T>
sortedList
(Collection<T> aCollection, String aKey) Returns a new list from the given list sorted by the given key.static <T> List<T>
sortedList
(Collection<T> aCollection, String... theKeys) Returns a new list from the given list sorted by the given key.void
Toggles the order for this sort between ORDER_ASCEND<->ORDER_DESCEND.toString()
Returns a string representation of sort (just the sort key).Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
ORDER_SAME
public static final byte ORDER_SAME- See Also:
-
ORDER_ASCEND
public static final byte ORDER_ASCEND- See Also:
-
ORDER_DESCEND
public static final byte ORDER_DESCEND- See Also:
-
COMPARATOR
-
-
Constructor Details
-
Sort
public Sort()Creates a plain sort with no key. Used for unarchival, shouldn't be called directly. -
Sort
Creates a sort with the given key and ORDER_ASCEND. -
Sort
Creates a sort with the given key and order.
-
-
Method Details
-
getKey
Returns the key for this sort. -
setKey
Sets the key for this sort. -
getOrder
public int getOrder()Returns the order for this sort. -
setOrder
public void setOrder(int anOrder) Sets the order for this sort. -
toggleOrder
public void toggleOrder()Toggles the order for this sort between ORDER_ASCEND<->ORDER_DESCEND. -
compare
Returns how the two given objects compare with this sort.- Specified by:
compare
in interfaceComparator
-
Compare
Compare two value objects (assumed to be String, Number, Boolean, Date, Comparable, etc.). -
sort
Returns the given list sorted by the given key. -
sort
Returns the given list sorted by the given sort. -
sort
Returns the given list sorted by the given key. -
sort
Returns the given list sorted by the given sort. -
sort
Returns the given list sorted by the given list of sorts. -
sortedList
Returns a new sorted list from given collection. -
sortedList
Returns a new list from the given list sorted by the given key. -
sortedList
Returns a new list from the given list sorted by the given key. -
clone
Standard clone implementation. -
getJSONKeys
RMJSONArchiver GetKeys method.- Specified by:
getJSONKeys
in interfaceJSArchiver.GetKeys
-
equals
Standard equals implementation.- Specified by:
equals
in interfaceComparator
- Overrides:
equals
in classObject
-
toString
Returns a string representation of sort (just the sort key).
-