thenComparator所在位置是kotlin.comparisons.thenComparator,其相关用法介绍如下。

用法:

inline fun <T> Comparator<T>.thenComparator(
    crossinline comparison: (a: T, b: T) -> Int
): Comparator<T>

使用主比较器和函数创建比较器以计算比较结果。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//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()
println(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()
println(map2) // {null=0, d=0, a=1, c=1, b=2}
//sampleEnd
}

输出:

{d=0, null=0, c=1, a=1, b=2}
{null=0, d=0, a=1, c=1, b=2}