【发布时间】:2022-11-04 15:46:17
【问题描述】:
对于 kotlin,它有 sortedByDescending 用于对列表进行排序。
如果列表中有一些空对象和一些具有一定值的对象,那么在排序时它想过滤掉那些项目,怎么做?
class TheObj (val postTime: Long, val tag: String)
val srcList = mutableListOf(
TheObj(2022, "a"),
TheObj(2020, "b"),
null,
TheObj(2021, "c"),
TheObj(2020, "invalid")
)
/////////////
// would like to filter out the null object and the object has tag=="invalid" in the sorted list
val desSortedList = srcList.sortedByDescending { obj -> obj.postTime }//<== this does not work
desSortedList.forEach{ s -> println(s.postTime) }
【问题讨论】: