Top Description Fields Constructors Methods
org.apache.commons.collections4

public Class ComparatorUtils

extends Object
Class Inheritance
Imports
java.util.Collection, .Comparator, org.apache.commons.collections4.comparators.BooleanComparator, .ComparableComparator, .ComparatorChain, .NullComparator, .ReverseComparator, .TransformingComparator

Provides convenient static utility methods for Comparator objects.

Most of the functionality in this class can also be found in the comparators package. This class merely provides a convenient central place if you have use for more than one class in the comparators subpackage.

Since
2.1

Field Summary

Modifier and TypeField and Description
public static final Comparator<T>
NATURAL_COMPARATOR

Comparator for natural sort order.

Constructor Summary

AccessConstructor and Description
private
ComparatorUtils()

ComparatorUtils should not normally be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static Comparator<Boolean>

Returns:

a comparator that sorts booleans
booleanComparator
(final boolean
when true, sort true Booleans before false Booleans.
trueFirst
)

Gets a Comparator that can sort Boolean objects.

public static <
the object type to compare
E
>
Comparator<E>

Returns:

a ComparatorChain formed from the input comparators
chainedComparator
(final Comparator<E>...
the comparators to use, not null or empty or containing nulls
comparators
)

Gets a comparator that compares using an array of Comparators, applied in sequence until one returns not equal or the array is exhausted.

public static <
the object type to compare
E
>
Comparator<E>

Returns:

a ComparatorChain formed from the input comparators
chainedComparator
(final Collection<Comparator<E>>
the comparators to use, not null or empty or containing nulls
comparators
)

Gets a comparator that compares using a collection of Comparators, applied in (default iterator) sequence until one returns not equal or the collection is exhausted.

public static <
the object type to compare
E
>
E

Returns:

the larger of the two objects
max
(final E
the first object to compare
o1
,
final E
the second object to compare
o2
,
Comparator<E>
the sort order to use
comparator
)

Returns the larger of the given objects according to the given comparator, returning the second object if the comparator returns equal.

public static <
the object type to compare
E
>
E

Returns:

the smaller of the two objects
min
(final E
the first object to compare
o1
,
final E
the second object to compare
o2
,
Comparator<E>
the sort order to use
comparator
)

Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.

public static <
the object type to compare
E extends Comparable<? super E>
>
Comparator<E>

Returns:

a comparator which uses natural order
naturalComparator
()

Gets a comparator that uses the natural order of the objects.

public static <
the object type to compare
E
>
Comparator<E>

Returns:

a version of that comparator that allows nulls
nullHighComparator
(Comparator<E>
the comparator that wants to allow nulls
comparator
)

Gets a Comparator that controls the comparison of null values.

public static <
the object type to compare
E
>
Comparator<E>

Returns:

a version of that comparator that allows nulls
nullLowComparator
(Comparator<E>
the comparator that wants to allow nulls
comparator
)

Gets a Comparator that controls the comparison of null values.

public static <
the object type to compare
E
>
Comparator<E>

Returns:

a comparator that reverses the order of the input comparator
reversedComparator
(final Comparator<E>
the comparator to reverse
comparator
)

Gets a comparator that reverses the order of the given comparator.

public static <
the input object type of the transformed comparator
I
,
the object type of the decorated comparator
O
>
Comparator<I>

Returns:

a comparator that transforms its input objects before comparing them
transformedComparator
(Comparator<O>
the sort order to use
comparator
,
final Transformer<? super I, ? extends O>
the transformer to use
transformer
)

Gets a Comparator that passes transformed objects to the given comparator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NATURAL_COMPARATORback to summary
public static final Comparator<T> NATURAL_COMPARATOR

Comparator for natural sort order.

Annotations
@SuppressWarnings:rawtypes, unchecked
See Also
ComparableComparator#comparableComparator()

Constructor Detail

ComparatorUtilsback to summary
private ComparatorUtils()

ComparatorUtils should not normally be instantiated.

Method Detail

booleanComparatorback to summary
public static Comparator<Boolean> booleanComparator(final boolean trueFirst)

Gets a Comparator that can sort Boolean objects.

The parameter specifies whether true or false is sorted first.

The comparator throws NullPointerException if a null value is compared.

Parameters
trueFirst:boolean

when true, sort true Booleans before false Booleans.

Returns:Comparator<Boolean>

a comparator that sorts booleans

chainedComparatorback to summary
public static <E> Comparator<E> chainedComparator(final Comparator<E>... comparators)

Gets a comparator that compares using an array of Comparators, applied in sequence until one returns not equal or the array is exhausted.

Parameters
<E>
the object type to compare
comparators:Comparator<E>[]

the comparators to use, not null or empty or containing nulls

Returns:Comparator<E>

a ComparatorChain formed from the input comparators

Exceptions
NullPointerException:
if comparators array is null or contains a null
See Also
ComparatorChain
chainedComparatorback to summary
public static <E> Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators)

Gets a comparator that compares using a collection of Comparators, applied in (default iterator) sequence until one returns not equal or the collection is exhausted.

Parameters
<E>
the object type to compare
comparators:Collection<Comparator<E>>

the comparators to use, not null or empty or containing nulls

Returns:Comparator<E>

a ComparatorChain formed from the input comparators

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
if comparators collection is null or contains a null
ClassCastException:
if the comparators collection contains the wrong object type
See Also
ComparatorChain
maxback to summary
public static <E> E max(final E o1, final E o2, Comparator<E> comparator)

Returns the larger of the given objects according to the given comparator, returning the second object if the comparator returns equal.

Parameters
<E>
the object type to compare
o1:E

the first object to compare

o2:E

the second object to compare

comparator:Comparator<E>

the sort order to use

Returns:E

the larger of the two objects

Annotations
@SuppressWarnings:unchecked
minback to summary
public static <E> E min(final E o1, final E o2, Comparator<E> comparator)

Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.

Parameters
<E>
the object type to compare
o1:E

the first object to compare

o2:E

the second object to compare

comparator:Comparator<E>

the sort order to use

Returns:E

the smaller of the two objects

Annotations
@SuppressWarnings:unchecked
naturalComparatorback to summary
public static <E extends Comparable<? super E>> Comparator<E> naturalComparator()

Gets a comparator that uses the natural order of the objects.

Parameters
<E>
the object type to compare
Returns:Comparator<E>

a comparator which uses natural order

Annotations
@SuppressWarnings:unchecked
nullHighComparatorback to summary
public static <E> Comparator<E> nullHighComparator(Comparator<E> comparator)

Gets a Comparator that controls the comparison of null values.

The returned comparator will consider a null value to be greater than any nonnull value, and equal to any other null value. Two nonnull values will be evaluated with the given comparator.

Parameters
<E>
the object type to compare
comparator:Comparator<E>

the comparator that wants to allow nulls

Returns:Comparator<E>

a version of that comparator that allows nulls

Annotations
@SuppressWarnings:unchecked
See Also
NullComparator
nullLowComparatorback to summary
public static <E> Comparator<E> nullLowComparator(Comparator<E> comparator)

Gets a Comparator that controls the comparison of null values.

The returned comparator will consider a null value to be less than any nonnull value, and equal to any other null value. Two nonnull values will be evaluated with the given comparator.

Parameters
<E>
the object type to compare
comparator:Comparator<E>

the comparator that wants to allow nulls

Returns:Comparator<E>

a version of that comparator that allows nulls

Annotations
@SuppressWarnings:unchecked
See Also
NullComparator
reversedComparatorback to summary
public static <E> Comparator<E> reversedComparator(final Comparator<E> comparator)

Gets a comparator that reverses the order of the given comparator.

Parameters
<E>
the object type to compare
comparator:Comparator<E>

the comparator to reverse

Returns:Comparator<E>

a comparator that reverses the order of the input comparator

See Also
ReverseComparator
transformedComparatorback to summary
public static <I, O> Comparator<I> transformedComparator(Comparator<O> comparator, final Transformer<? super I, ? extends O> transformer)

Gets a Comparator that passes transformed objects to the given comparator.

Objects passed to the returned comparator will first be transformed by the given transformer before they are compared by the given comparator.

Parameters
<I>
the input object type of the transformed comparator
<O>
the object type of the decorated comparator
comparator:Comparator<O>

the sort order to use

transformer:Transformer<? super I, ? extends O>

the transformer to use

Returns:Comparator<I>

a comparator that transforms its input objects before comparing them

Annotations
@SuppressWarnings:unchecked
See Also
TransformingComparator