假设我有一个以任意顺序定义的值列表。
val list = listOf("a", "aa", "b", "bb")
如果我使用值的某些 Comparable
属性使用 sortedBy
对此列表进行排序。
val sorted = list.sortedBy { it.length }
对于比较相等的值,是否会保留原始任意顺序?
["a", "b", "aa", "bb"] // good
["b", "a", "aa", "bb"] // bad
["a", "b", "bb", "aa"] // bad
["b", "a", "bb", "aa"] // bad
最佳答案
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
关于sorting - Kotlin 的sortedBy 是否保持相等值的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61646062/