then Comparator
inline fun <T> Comparator<T>.thenComparator(crossinline comparison: (T, T) -> Int): Comparator<T>
Content copied to clipboard
Creates a comparator using the primary comparator and function to calculate a result of comparison.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf("c" to 1, "b" to 2, "a" to 1, "d" to 0, null to 0)
val valueComparator = compareBy<Pair<String?, Int>> { it.second }
val map1 = list.sortedWith(valueComparator).toMap()
assertPrints(map1, "{d=0, null=0, c=1, a=1, b=2}")
val valueThenKeyComparator = valueComparator
.thenComparator({ a, b -> compareValues(a.first, b.first) })
val map2 = list.sortedWith(valueThenKeyComparator).toMap()
assertPrints(map2, "{null=0, d=0, a=1, c=1, b=2}")
//sampleEnd
}