spring-framework / org.springframework.util.comparator / CompoundComparator

CompoundComparator

open class CompoundComparator<T : Any> : Comparator<T>, Serializable

A comparator that chains a sequence of one or more Comparators.

A compound comparator calls each Comparator in sequence until a single Comparator returns a non-zero result, or the comparators are exhausted and zero is returned.

This facilitates in-memory sorting similar to multi-column sorting in SQL. The order of any single Comparator in the list can also be reversed.

Author
Keith Donald

Author
Juergen Hoeller

Since
1.2.2

Constructors

<init>

CompoundComparator()

Construct a CompoundComparator with initially no Comparators. Clients must add at least one Comparator before calling the compare method or an IllegalStateException is thrown.

CompoundComparator(vararg comparators: Comparator<Any>)

Construct a CompoundComparator from the Comparators in the provided array.

All Comparators will default to ascending sort order, unless they are InvertibleComparators.

Functions

addComparator

open fun addComparator(comparator: Comparator<out T>): Unit

Add a Comparator to the end of the chain.

The Comparator will default to ascending sort order, unless it is a InvertibleComparator.

open fun addComparator(comparator: Comparator<out T>, ascending: Boolean): Unit

Add a Comparator to the end of the chain using the provided sort order.

compare

open fun compare(o1: T, o2: T): Int

equals

open fun equals(other: Any?): Boolean

getComparatorCount

open fun getComparatorCount(): Int

Returns the number of aggregated comparators.

hashCode

open fun hashCode(): Int

invertOrder

open fun invertOrder(): Unit

Invert the sort order of each sort definition contained by this compound comparator.

open fun invertOrder(index: Int): Unit

Invert the sort order of the sort definition at the specified index.

setAscendingOrder

open fun setAscendingOrder(index: Int): Unit

Change the sort order at the given index to ascending.

setComparator

open fun setComparator(index: Int, comparator: Comparator<out T>): Unit

Replace the Comparator at the given index.

The Comparator will default to ascending sort order, unless it is a InvertibleComparator.

open fun setComparator(index: Int, comparator: Comparator<T>, ascending: Boolean): Unit

Replace the Comparator at the given index using the given sort order.

setDescendingOrder

open fun setDescendingOrder(index: Int): Unit

Change the sort order at the given index to descending sort.

toString

open fun toString(): String