这是列表:
[Videos(id=5, yt_id=yRPUkDjwr1A, title=test5, likes=0, kat=animals), Videos(id=4, yt_id=yRPUkDjwr1A, title=test4, likes=1, kat=pranks), Videos(id=3, yt_id=WkyUU9ZDUto, title=test3, likes=1, kat=pranks), Videos(id=2, yt_id=B_X9OQqtduE, title=test2, likes=0, kat=animals), Videos(id=1, yt_id=ywaKlGNiv80, title=test1, likes=0, kat=animals)]
如何按likes
desc对其进行排序?
谢谢!
答案 0 :(得分:1)
您可以使用sortByDescending:
videos.sortByDescending { it.likes }
我认为这是自我解释。
如果要将排序结果分配给另一个列表,请使用sortedByDescending:
val sortedList = videos.sortedByDescending { it.likes }
结果列表的推断类型为List<Videos>
,但是如果需要MutableList
:
val sortedList = videos.sortedByDescending { it.likes }.toMutableList()