Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Kotlin中更改compareBy的顺序_Android_Kotlin_Comparable - Fatal编程技术网

Android 如何在Kotlin中更改compareBy的顺序

Android 如何在Kotlin中更改compareBy的顺序,android,kotlin,comparable,Android,Kotlin,Comparable,我需要一个比较器,在Kotlin中,对进入我的回收器视图的每个对象进行排序(使用快速适配器) 我需要按整数对对象排序,最大的先到 上面的代码对进入我的列表的对象进行排序,但最大的在列表的末尾。有没有办法颠倒顺序 playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true) 您可以始终使用compareByDescending您可以始终按照上的建议使

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

我需要按整数对对象排序,最大的先到

上面的代码对进入我的列表的对象进行排序,但最大的在列表的末尾。有没有办法颠倒顺序

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

您可以始终使用
compareByDescending

您可以始终按照上的建议使用
compareByDescending

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

现在它正按照我想要的方式工作

如上所示:

mylist.sortWith(compareBy<Player> { it.name }.thenBy { it.age }.thenBy { it.gender })
更改为
compareBy({-it.player.goals},{-it.player.assists})

现在它正按照我想要的方式工作

mylist.sortWith(比较方式为{it.name}.thenBy{it.age}.thenBy{it.gender})
mylist.sortWith(compareBy<Player> { it.name }.thenBy { it.age }.thenBy { it.gender })

mylist.sortWith(compareByDescending{it.name}
.thenBy降序{it.age}.thenBy{it.gender})
mylist.sortWith(比较方式为{it.name}.thenBy{it.age}.thenBy{it.gender})

mylist.sortWith(compareByDescending{it.name}
.thenBy降序{it.age}.thenBy{it.gender})

感谢您的帮助,但比较搜索不支持多个字段进行比较。感谢您的帮助,但比较搜索不支持多个字段进行比较。有关Kotlin排序的有用问题:关于Kotlin排序的有用问题:请正确格式化您的代码。并对代码进行一些解释,而不是转储代码。我格式化了代码,但不确定它是否正确。至少,它返回的是
Unit
,而不是list。最好看一下。请正确格式化您的代码。并对代码进行一些解释,而不是转储代码。我格式化了代码,但不确定它是否正确。至少,它返回的是
Unit
,而不是list。最好看看。