如何在 Kotlin 中更改compareBy的顺序

Gui*_*ira 5 android comparable kotlin

我需要一个 Kotlin 中的比较器来对进入我的回收器视图中的每个对象进行排序(使用快速适配器)。

我需要按整数对对象进行排序,其中最大的排在前面。

上面的代码对进入列表的对象进行排序,但最大的对象位于列表的末尾。有办法颠倒顺序吗?

playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true)
Run Code Online (Sandbox Code Playgroud)

小智 7

mylist.sortWith(compareBy<Player> { it.name }.thenBy { it.age }.thenBy { it.gender })
Run Code Online (Sandbox Code Playgroud)

或者

mylist.sortWith(compareByDescending<Player> { it.name }
    .thenByDescending { it.age }.thenBy { it.gender })
Run Code Online (Sandbox Code Playgroud)

  • 请正确格式化您的代码。并对代码进行一些解释,而不是单独转储代码 (2认同)