【问题标题】:Kotin sort by descending then ascendingKotlin 按降序然后升序排序
【发布时间】:2020-04-07 21:42:40
【问题描述】:

我正在尝试对多个参数的列表进行排序。例如,一个值降序,第二个值升序,第三个值降序。

 var people = listOf<People>(People("palazo", "ooo", 1),
            People("asdf", "cccc", 2),
            People("Maria", "ooo", 3),
            People("Helena", "ccccc", 3),
            People("Carlos", "ccc", 4),
            People("Jhon", "ooo", 2)
    )

有这样的方法吗? (我知道是不正确的) people = people.sortedByDescending {it.name}.thenBy{it.lastname}.thenDescending{it.age}

或添加到:

   people.sortedWith(compareBy(People::name, People::lastName)) 
   //but mixing ascending and descending

【问题讨论】:

  • 你想达到什么目的?您能否详细说明并提供更多模型数据?

标签: sorting kotlin collections


【解决方案1】:
val sortedPeople = people.sortedWith(
    compareByDescending(People::name)
        .thenBy(People::lastName)
        .thenByDescending(People::age)
)

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2012-11-08
    相关资源
    最近更新 更多