简体   繁体   English

如何在Kotlin中更改compareBy的顺序

[英]How to change the order of compareBy in Kotlin

I need a comparator, in Kotlin, to order every object that enter in my recycler view (using Fast Adapter). 在Kotlin中,我需要一个比较器来排序进入回收器视图中的每个对象(使用快速适配器)。

I need to order the objects by an Integer, where the biggest ones come first. 我需要按整数排序对象,最大的对象在前。

The code above order the object that enters in my list, but the biggest ones is in the end of the list. 上面的代码对输入到我的列表中的对象进行了排序,但是最大的代码在列表的末尾。 Is there a way to reverse the order? 有没有办法撤销订单?

playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true)

As suggested by Mitja Slenc on Kotlin forum : 正如Mitja SlencKotlin论坛上建议的那样

Changed to compareBy({ -it.player.goals}, {-it.player.assists}) 更改为compareBy({ -it.player.goals}, {-it.player.assists})

And now it's working the way I wanted! 现在它以我想要的方式工作!

mylist.sortWith(compareBy<Player> { it.name }.thenBy { it.age }.thenBy { it.gender })

or 要么

mylist.sortWith(compareByDescending<Player> { it.name }
    .thenByDescending { it.age }.thenBy { it.gender })

您可以随时使用compareByDescending

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM