Comparator

Platform and version requirements: JS (1.1), Native (1.3)
fun interface Comparator<T>
Platform and version requirements: JVM (1.1)
typealias Comparator<T> = Comparator<T>

Provides a comparison function for imposing a total ordering between instances of the type T.

Functions

Platform and version requirements: JS (1.0), Native (1.0)

compare

Compares its two arguments for order. Returns zero if the arguments are equal, a negative number if the first argument is less than the second, or a positive number if the first argument is greater than the second.

abstract fun compare(a: T, b: T): Int

Extension Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

reversed

Returns a comparator that imposes the reverse ordering of this comparator.

fun <T> Comparator<T>.reversed(): Comparator<T>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

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>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

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>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

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>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

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>
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

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>

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-comparator/index.html