Kir*_*man 68 comparable kotlin
假设我有一个class Foo(val a: String, val b: Int, val c: Date)
,我想Foo
根据所有三个属性对s 列表进行排序.我该怎么做?
Kir*_*man 119
Kotlin的stdlib为此提供了许多有用的辅助方法.
首先,您可以使用该compareBy()
方法定义比较器并将其传递给sortedWith()
扩展方法以接收列表的已排序副本:
val list: List<Foo> = ...
val sortedList = list.sortedWith(compareBy({ it.a }, { it.b }, { it.c }))
Run Code Online (Sandbox Code Playgroud)
其次,你可以让Foo
实现Comparable<Foo>
使用compareValuesBy()
helper方法:
class Foo(val a: String, val b: Int, val c: Date) : Comparable<Foo> {
override fun compareTo(other: Foo)
= compareValuesBy(this, other, { it.a }, { it.b }, { it.c })
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以调用sorted()
不带参数的扩展方法来接收列表的排序副本:
val sortedList = list.sorted()
Run Code Online (Sandbox Code Playgroud)
如果您需要对某些值进行升序排序并降序其他值,则stdlib还提供以下功能:
list.sortedWith(compareBy<Foo> { it.a }.thenByDescending { it.b }.thenBy { it.c })
Run Code Online (Sandbox Code Playgroud)
该vararg
版本compareValuesBy
中的字节代码的意思匿名类是不是内联将为lambda表达式生成.但是,如果lambdas本身不捕获状态,则将使用单例实例而不是每次实例化lambdas.
正如Paul Woitaschek在评论中所指出的,与多个选择器相比,每次都会为vararg调用实例化一个数组.您不能通过提取数组来优化它,因为它将在每次调用时被复制.另一方面,您可以将逻辑提取到静态比较器实例中并重用它:
class Foo(val a: String, val b: Int, val c: Date) : Comparable<Foo> {
override fun compareTo(other: Foo) = comparator.compare(this, other)
companion object {
// using the method reference syntax as an alternative to lambdas
val comparator = compareBy(Foo::a, Foo::b, Foo::c)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20817 次 |
最近记录: |