Package kotlin.comparisons
Helper functions for creating Comparator instances.
Helper functions for creating Comparator instances.
Functions
compareBy
Creates a comparator using the sequence of functions to calculate a result of comparison.
The functions are called sequentially, receive the given values a and b and return Comparable
objects. As soon as the Comparable instances returned by a function for a and b values do not
compare as equal, the result of that comparison is returned from the Comparator.
fun <T> compareBy(
vararg selectors: (T) -> Comparable<*>?
): Comparator<T>Creates a comparator using the function to transform value to a Comparable instance for comparison.
fun <T> compareBy(
selector: (T) -> Comparable<*>?
): Comparator<T>Creates a comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.
fun <T, K> compareBy(
comparator: Comparator<in K>,
selector: (T) -> K
): Comparator<T>compareByDescending
Creates a descending comparator using the function to transform value to a Comparable instance for comparison.
fun <T> compareByDescending(
selector: (T) -> Comparable<*>?
): Comparator<T>Creates a descending comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.
fun <T, K> compareByDescending(
comparator: Comparator<in K>,
selector: (T) -> K
): Comparator<T>compareValues
Compares two nullable Comparable values. Null is considered less than any value.
fun <T : Comparable<*>> compareValues(a: T?, b: T?): IntcompareValuesBy
Compares two values using the specified functions selectors to calculate the result of the comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned.
fun <T> compareValuesBy(
a: T,
b: T,
vararg selectors: (T) -> Comparable<*>?
): IntCompares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return Comparable objects. The result of comparison of these Comparable instances is returned.
fun <T> compareValuesBy(
a: T,
b: T,
selector: (T) -> Comparable<*>?
): IntCompares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return objects of type K which are then being compared with the given comparator.
fun <T, K> compareValuesBy(
a: T,
b: T,
comparator: Comparator<in K>,
selector: (T) -> K
): IntmaxOf
Returns the greater of three values according to the order specified by the given comparator.
fun <T> maxOf(
a: T,
b: T,
c: T,
comparator: Comparator<in T>
): TReturns the greater of two values according to the order specified by the given comparator. If values are equal, returns the first one.
fun <T> maxOf(a: T, b: T, comparator: Comparator<in T>): TReturns the greater of two values.
fun maxOf(a: Byte, b: Byte): Bytefun maxOf(a: Short, b: Short): Shortfun maxOf(a: Int, b: Int): Intfun maxOf(a: Long, b: Long): Longfun maxOf(a: Float, b: Float): Floatfun maxOf(a: Double, b: Double): DoubleReturns the greater of three values.
fun <T : Comparable<T>> maxOf(a: T, b: T, c: T): Tfun maxOf(a: Byte, b: Byte, c: Byte): Bytefun maxOf(a: Short, b: Short, c: Short): Shortfun maxOf(a: Int, b: Int, c: Int): Intfun maxOf(a: Long, b: Long, c: Long): Longfun maxOf(a: Float, b: Float, c: Float): Floatfun maxOf(a: Double, b: Double, c: Double): DoubleReturns the greater of two values. If values are equal, returns the first one.
fun <T : Comparable<T>> maxOf(a: T, b: T): TminOf
Returns the smaller of three values according to the order specified by the given comparator.
fun <T> minOf(
a: T,
b: T,
c: T,
comparator: Comparator<in T>
): TReturns the smaller of two values according to the order specified by the given comparator. If values are equal, returns the first one.
fun <T> minOf(a: T, b: T, comparator: Comparator<in T>): TReturns the smaller of two values.
fun minOf(a: Byte, b: Byte): Bytefun minOf(a: Short, b: Short): Shortfun minOf(a: Int, b: Int): Intfun minOf(a: Long, b: Long): Longfun minOf(a: Float, b: Float): Floatfun minOf(a: Double, b: Double): DoubleReturns the smaller of three values.
fun <T : Comparable<T>> minOf(a: T, b: T, c: T): Tfun minOf(a: Byte, b: Byte, c: Byte): Bytefun minOf(a: Short, b: Short, c: Short): Shortfun minOf(a: Int, b: Int, c: Int): Intfun minOf(a: Long, b: Long, c: Long): Longfun minOf(a: Float, b: Float, c: Float): Floatfun minOf(a: Double, b: Double, c: Double): DoubleReturns the smaller of two values. If values are equal, returns the first one.
fun <T : Comparable<T>> minOf(a: T, b: T): TnaturalOrder
Returns a comparator that compares Comparable objects in natural order.
fun <T : Comparable<T>> naturalOrder(): Comparator<T>nullsFirst
Extends the given comparator of non-nullable values to a comparator of nullable values
considering null value less than any other value.
fun <T : Any> nullsFirst(
comparator: Comparator<in T>
): Comparator<T?>Provides a comparator of nullable Comparable values
considering null value less than any other value.
fun <T : Comparable<T>> nullsFirst(): Comparator<T?>nullsLast
Extends the given comparator of non-nullable values to a comparator of nullable values
considering null value greater than any other value.
fun <T : Any> nullsLast(
comparator: Comparator<in T>
): Comparator<T?>Provides a comparator of nullable Comparable values
considering null value greater than any other value.
fun <T : Comparable<T>> nullsLast(): Comparator<T?>reversed
Returns a comparator that imposes the reverse ordering of this comparator.
fun <T> Comparator<T>.reversed(): Comparator<T>reverseOrder
Returns a comparator that compares Comparable objects in reversed natural order.
fun <T : Comparable<T>> reverseOrder(): Comparator<T>then
Combines this comparator and the given comparator such that the latter is applied only when the former considered values equal.
infix fun <T> Comparator<T>.then(
comparator: Comparator<in T>
): Comparator<T>thenBy
Creates a comparator comparing values after the primary comparator defined them equal. It uses the function to transform value to a Comparable instance for comparison.
fun <T> Comparator<T>.thenBy(
selector: (T) -> Comparable<*>?
): Comparator<T>Creates a comparator comparing values after the primary comparator defined them equal. It uses the selector function to transform values and then compares them with the given comparator.
fun <T, K> Comparator<T>.thenBy(
comparator: Comparator<in K>,
selector: (T) -> K
): Comparator<T>thenByDescending
Creates a descending comparator using the primary comparator and the function to transform value to a Comparable instance for comparison.
fun <T> Comparator<T>.thenByDescending(
selector: (T) -> Comparable<*>?
): Comparator<T>Creates a descending comparator comparing values after the primary comparator defined them equal. It uses the selector function to transform values and then compares them with the given comparator.
fun <T, K> Comparator<T>.thenByDescending(
comparator: Comparator<in K>,
selector: (T) -> K
): Comparator<T>thenComparator
Creates a comparator using the primary comparator and function to calculate a result of comparison.
fun <T> Comparator<T>.thenComparator(
comparison: (a: T, b: T) -> Int
): Comparator<T>thenDescending
Combines this comparator and the given comparator such that the latter is applied only when the former considered values equal.
infix fun <T> Comparator<T>.thenDescending(
comparator: Comparator<in T>
): Comparator<T>