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

public Class ReverseComparator<E>

extends Object
implements Comparator<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Comparator
Type Parameters
<E>
the type of objects compared by this comparator
Imports
java.io.Serializable, java.util.Comparator, org.apache.commons.collections4.ComparatorUtils

Reverses the order of another comparator by reversing the arguments to its compare method.
Since
2.0
See Also
java.util.Collections#reverseOrder()

Field Summary

Modifier and TypeField and Description
private final Comparator<? super E>
comparator

The comparator being decorated.

private static final long
serialVersionUID

Serialization version from Collections 2.0.

Constructor Summary

AccessConstructor and Description
public
ReverseComparator()

Creates a comparator that compares objects based on the inverse of their natural ordering.

public
ReverseComparator(final Comparator<? super E>
Comparator to reverse
comparator
)

Creates a comparator that inverts the comparison of the given comparator.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

negative if obj1 is less, positive if greater, zero if equal
compare
(final E
the first object to compare
obj1
,
final E
the second object to compare
obj2
)

Implements java.util.Comparator.compare.

Compares two objects in reverse order.
public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
object
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.
public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private final Comparator<? super E> comparator

The comparator being decorated.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version from Collections 2.0.

Constructor Detail

ReverseComparatorback to summary
public ReverseComparator()

Creates a comparator that compares objects based on the inverse of their natural ordering. Using this Constructor will create a ReverseComparator that is functionally identical to the Comparator returned by java.util.Collections.reverseOrder().

See Also
java.util.Collections#reverseOrder()
ReverseComparatorback to summary
public ReverseComparator(final Comparator<? super E> comparator)

Creates a comparator that inverts the comparison of the given comparator. If you pass in null, the ReverseComparator defaults to reversing the natural order, as per java.util.Collections#reverseOrder().

Parameters
comparator:Comparator<? super E>

Comparator to reverse

Method Detail

compareback to summary
public int compare(final E obj1, final E obj2)

Implements java.util.Comparator.compare.

Compares two objects in reverse order.

Parameters
obj1:E

the first object to compare

obj2:E

the second object to compare

Returns:int

negative if obj1 is less, positive if greater, zero if equal

Annotations
@Override
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.

This implementation returns true iff object.getClass() equals this.getClass(), and the underlying comparators are equal. Subclasses may want to override this behavior to remain consistent with the equals contract.

Parameters
object:Object

the object to compare to

Returns:boolean

true if equal

Annotations
@Override
Since
3.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals.

Returns:int

a suitable hash code

Annotations
@Override
Since
3.0