我有一个包含键和值的映射.我想将其传输到一个列表,并首先按值(整数)排序,然后按键(字符串)排序.这是,它按值排序,但如果值中有关联,我想通过按字母顺序排序来"解开"它.有没有什么Kotlin、Java函数可以做到这一点? 提前谢谢你

推荐答案

存在实现比较器Comparator for Pair in Kotlin的可能性

这应该是可行的,因为yourMap.toList()是一个Pair的列表. 唯一需要的更改是更改第一个和第二个比较器密钥,如下所示(改编自first answer)

fun <T, U> pairComparator(
    firstComparator: Comparator<T>, 
    secondComparator: Comparator<U>
): Comparator<Pair<T, U>> = 
         compareBy(firstComparator) { p: Pair<T, U> -> p.second }
          .thenBy(secondComparator) { p: Pair<T, U> -> p.first }

Kotlin相关问答推荐

Gradle Jooq配置自定义生成器

如何创建一个空的kotlin工作?

列表在 android WebView 中没有正确迭代

Criteria Api 中的 Kotlin 泛型

在 kotlin 中使具体化字段可选

Rabin-Karp字符串匹配的实现(Rolling hash)

为什么这两个 Kotlin 协程函数不一样?

Kotlin:我可以将函数分配给 main 的伴随对象中的变量吗?

kotest 更改环境变量

如何在 Compose 中创建可重用的修饰符?

Kotlin 中多个 init 块的用例?

Gradle 同步失败:不支持的方法:KotlinPlatformContainer.supports()

如何使 TextInputEditText 只读?

在 Kotlin 中取最后 n 个元素

Kotlin的web-framework框架

ObjectAnimator.ofFloat 不能直接在kotlin中取Int作为参数

类型不匹配推断类型为单位,但应为空

查找是否在列表中找到具有特定属性值的元素

在 Kotlin 中声明 Byte 会出现编译时错误The integer literal does not conform to the expected type Byte

var str:String是可变的还是不可变的?