我无法按照日期降序排序一个对象列表。
假设这是我的类Thing
:
class Thing {
Profil profil
String status = 'ready'
Date dtCreated = new Date()
}
在这个方法内我正在创建List things
List profiles = profil.xyz?.collect { Profil.collection.findOne(_id:it) }
List things = []
然后我使用每个配置文件中关联的Thing
填充列表
profiles.each() { profile,i ->
if(profile) {
things += Thing.findAllByProfilAndStatus(profile, "ready", [sort: 'dtCreated', order: 'desc']) as
}
好的,现在things
里有很多东西,不幸的是[order: 'desc']
被应用到每个物品集上,我需要按dtCreated
对整个列表进行排序。这就像下面这样工作得很好:
things.sort{it.dtCreated}
好的,现在所有的事情都已按日期排序,但顺序错误,最近的事情是列表中的最后一件事。
所以我需要按相反的方向排序,我没有找到任何能帮助我的网络资源,我尝试了一些方法,比如
things.sort{-it.dtCreated} //doesnt work
things.sort{it.dtCreated}.reverse() //has no effect
我希望能够找到一种优雅的方式来按日期降序排序我的事情,也许有人知道我该如何做?类似于我之前使用的ORM [sort: 'dtCreated', order: 'desc']
,难道没有其他方法吗?