public abstract class Ordering<T> extends Object implements Comparator<T>
Comparator
, in the same sense that FluentIterable
is an enriched Iterable
.
The common ways to get an instance of Ordering
are:
compare(T, T)
instead of implementing
Comparator
directly
Comparator
instance to from(Comparator)
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
.
Modifier and Type | Method and Description |
---|---|
abstract int |
compare(T left,
T right) |
static <T> Ordering<T> |
from(Comparator<T> comparator)
Returns an ordering based on an existing comparator instance.
|
static <C extends Comparable> |
natural()
Returns a serializable ordering that uses the natural order of the values.
|
<S extends T> |
reverse()
Returns the reverse of this ordering; the
Ordering equivalent to
Collections.reverseOrder(Comparator) . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public static <C extends Comparable> Ordering<C> natural()
NullPointerException
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.
public static <T> Ordering<T> from(Comparator<T> comparator)
Comparator
just to pass it in here. Instead, simply
subclass Ordering
and implement its compare
method
directly.comparator
- the comparator that defines the orderOrdering
; otherwise
an ordering that wraps that comparatorpublic <S extends T> Ordering<S> reverse()
Ordering
equivalent to
Collections.reverseOrder(Comparator)
.public abstract int compare(T left, T right)
compare
in interface Comparator<T>
Copyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.