How to sort based on/compare multiple values in Kotlin?

前端 未结 3 525
说谎
说谎 2020-11-27 03:17

Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foos based on all three properties. How would I go abou

3条回答
  •  时光说笑
    2020-11-27 04:03

    If you need sort by multiple fields, and some field by descending and others by ascending you could use:

    YOUR_MUTABLE_LIST.sortedWith(compareBy { it.PARAM_1}.thenByDescending { it.PARAM_2}.thenBy { it.PARAM_3})
    

提交回复
热议问题