Package kotlin.comparisons

Helper functions for creating Comparator instances.

Extensions for External Classes

java.util.Comparator

Functions

compareBy

fun <T> compareBy(
    vararg selectors: (T) -> Comparable<*>?
): Comparator<T>

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(
    selector: (T) -> Comparable<*>?
): Comparator<T>

Creates a comparator using the function to transform value to a Comparable instance for comparison.

fun <T, K> compareBy(
    comparator: Comparator<in K>,
    selector: (T) -> K
): Comparator<T>

Creates a comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.

compareByDescending

fun <T> compareByDescending(
    selector: (T) -> Comparable<*>?
): Comparator<T>

Creates a descending comparator using the function to transform value to a Comparable instance for comparison.

fun <T, K> compareByDescending(
    comparator: Comparator<in K>,
    selector: (T) -> K
): 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.

compareValues

fun <T : Comparable<*>> compareValues(a: T?, b: T?): Int

Compares two nullable Comparable values. Null is considered less than any value.

compareValuesBy

fun <T> compareValuesBy(
    a: T,
    b: T,
    vararg selectors: (T) -> Comparable<*>?
): Int

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,
    selector: (T) -> Comparable<*>?
): Int

Compares 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, K> compareValuesBy(
    a: T,
    b: T,
    comparator: Comparator<in K>,
    selector: (T) -> K
): Int

Compares 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.

maxOf

actual fun <T : Comparable<T>> maxOf(a: T, b: T): T

Returns the greater of two values. If values are equal, returns the first one.

actual fun maxOf(a: Byte, b: Byte): Byte
actual fun maxOf(a: Short, b: Short): Short
actual fun maxOf(a: Int, b: Int): Int
actual fun maxOf(a: Long, b: Long): Long
actual fun maxOf(a: Float, b: Float): Float
actual fun maxOf(a: Double, b: Double): Double

Returns the greater of two values.

actual fun <T : Comparable<T>> maxOf(a: T, b: T, c: T): T
actual fun maxOf(a: Byte, b: Byte, c: Byte): Byte
actual fun maxOf(a: Short, b: Short, c: Short): Short
actual fun maxOf(a: Int, b: Int, c: Int): Int
actual fun maxOf(a: Long, b: Long, c: Long): Long
actual fun maxOf(a: Float, b: Float, c: Float): Float
actual fun maxOf(a: Double, b: Double, c: Double): Double

Returns the greater of three values.

fun <T> maxOf(
    a: T,
    b: T,
    c: T,
    comparator: Comparator<in T>
): T

Returns the greater of three values according to the order specified by the given comparator.

fun <T> maxOf(a: T, b: T, comparator: Comparator<in T>): T

Returns the greater of two values according to the order specified by the given comparator. If values are equal, returns the first one.

minOf

actual fun <T : Comparable<T>> minOf(a: T, b: T): T

Returns the smaller of two values. If values are equal, returns the first one.

actual fun minOf(a: Byte, b: Byte): Byte
actual fun minOf(a: Short, b: Short): Short
actual fun minOf(a: Int, b: Int): Int
actual fun minOf(a: Long, b: Long): Long
actual fun minOf(a: Float, b: Float): Float
actual fun minOf(a: Double, b: Double): Double

Returns the smaller of two values.

actual fun <T : Comparable<T>> minOf(a: T, b: T, c: T): T
actual fun minOf(a: Byte, b: Byte, c: Byte): Byte
actual fun minOf(a: Short, b: Short, c: Short): Short
actual fun minOf(a: Int, b: Int, c: Int): Int
actual fun minOf(a: Long, b: Long, c: Long): Long
actual fun minOf(a: Float, b: Float, c: Float): Float
actual fun minOf(a: Double, b: Double, c: Double): Double

Returns the smaller of three values.

fun <T> minOf(
    a: T,
    b: T,
    c: T,
    comparator: Comparator<in T>
): T

Returns the smaller of three values according to the order specified by the given comparator.

fun <T> minOf(a: T, b: T, comparator: Comparator<in T>): T

Returns the smaller of two values according to the order specified by the given comparator. If values are equal, returns the first one.

naturalOrder

fun <T : Comparable<T>> naturalOrder(): Comparator<T>

Returns a comparator that compares Comparable objects in natural order.

nullsFirst

fun <T : Any> nullsFirst(
    comparator: Comparator<in T>
): Comparator<T?>

Extends the given comparator of non-nullable values to a comparator of nullable values considering null value less than any other value.

fun <T : Comparable<T>> nullsFirst(): Comparator<T?>

Provides a comparator of nullable Comparable values considering null value less than any other value.

nullsLast

fun <T : Any> nullsLast(
    comparator: Comparator<in T>
): Comparator<T?>

Extends the given comparator of non-nullable values to a comparator of nullable values considering null value greater than any other value.

fun <T : Comparable<T>> nullsLast(): Comparator<T?>

Provides a comparator of nullable Comparable values considering null value greater than any other value.

reverseOrder

fun <T : Comparable<T>> reverseOrder(): Comparator<T>

Returns a comparator that compares Comparable objects in reversed natural order.