Class Ordering<T>
- All Implemented Interfaces:
Comparator<T>
Comparator
, in the same sense that FluentIterable
is an enriched Iterable
.
The common ways to get an instance of Ordering
are:
- Subclass it and implement
compare(T, T)
instead of implementingComparator
directly - Pass a pre-existing
Comparator
instance tofrom(Comparator)
- Use the natural ordering,
natural()
Then you can use the chaining methods to get an altered version of
that Ordering
, including:
reverse()
#compound(Comparator)
#onResultOf(Function)
nullsFirst()
/nullsLast()
Finally, use the resulting Ordering
anywhere a Comparator
is required, or use any of its special operations, such as:
#immutableSortedCopy
#isOrdered
/#isStrictlyOrdered
min(java.util.Iterator<E>)
/max(java.util.Iterator<E>)
Except as noted, the orderings returned by the factory methods of this
class are serializable if and only if the provided instances that back them
are. For example, if ordering
and function
can themselves be
serialized, then ordering.onResultOf(function)
can as well.
See the Guava User Guide article on
Ordering
.
- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Jesse Wilson, Kevin Bourrillion
-
Method Summary
Modifier and TypeMethodDescriptionabstract int
static <T> Ordering<T>
from
(Comparator<T> comparator) Returns an ordering based on an existing comparator instance.static <C extends Comparable>
Ordering<C>natural()
Returns a serializable ordering that uses the natural order of the values.reverse()
Returns the reverse of this ordering; theOrdering
equivalent toCollections.reverseOrder(Comparator)
.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, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Method Details
-
natural
Returns a serializable ordering that uses the natural order of the values. The ordering throws aNullPointerException
when passed a null parameter.The type specification is
<C extends Comparable>
, instead of the technically correct<C extends Comparable<? super C>>
, to support legacy types from before Java 5. -
from
Returns an ordering based on an existing comparator instance. Note that it is unnecessary to create a new anonymous inner class implementingComparator
just to pass it in here. Instead, simply subclassOrdering
and implement itscompare
method directly.- Parameters:
comparator
- the comparator that defines the order- Returns:
- comparator itself if it is already an
Ordering
; otherwise an ordering that wraps that comparator
-
reverse
Returns the reverse of this ordering; theOrdering
equivalent toCollections.reverseOrder(Comparator)
. -
compare
- Specified by:
compare
in interfaceComparator<T>
-