Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Java 按日期降序排序列表-groovy疯狂_Java_Date_Sorting_Grails_Groovy - Fatal编程技术网

Java 按日期降序排序列表-groovy疯狂

Java 按日期降序排序列表-groovy疯狂,java,date,sorting,grails,groovy,Java,Date,Sorting,Grails,Groovy,我无法按日期降序排列对象列表 假设这是我的班级事情 class Thing { Profil profil String status = 'ready' Date dtCreated = new Date() } 在方法内部,我正在创建列表 List profiles = profil.xyz?.collect { Profil.collection.findOne(_id:it) } List things = [] 然后,我用每个配置

我无法按日期降序排列对象列表

假设这是我的班级
事情

class Thing {

Profil profil
String status = 'ready'
Date dtCreated = new Date()
}
在方法内部,我正在创建
列表

            List profiles = profil.xyz?.collect { Profil.collection.findOne(_id:it) }

            List things = []
然后,我用每个配置文件的每个关联的
东西
填充列表

            profiles.each() { profile,i ->
                if(profile) {
                    things += Thing.findAllByProfilAndStatus(profile, "ready", [sort: 'dtCreated', order: 'desc']) as 
                 }
好的,现在
东西
中有很多东西,不幸的是
[顺序:'desc']
应用于每一组东西,我需要按照
dtCreated对整个列表进行排序。那工作起来很好,就像

            things.sort{it.dtCreated}
好吧,现在所有的东西都是按日期排序的,但顺序不对,最近的东西是列表中的最后一个

所以我需要按相反的方向排序,我在网上找不到任何让我进步的东西,我尝试了类似的东西

            things.sort{-it.dtCreated} //doesnt work
            things.sort{it.dtCreated}.reverse() //has no effect
我没有找到任何groovy方法来实现这样的标准操作,也许有人告诉我如何按日期降序排序?必须有类似于我在上面使用的orm的东西
[排序:'dtCreated',排序:'desc']
或者不是吗?

而不是

things.sort{-it.dtCreated}
你可以试试

things.sort{a,b-> b.dtCreated<=>a.dtCreated}
应该有用

things = things.reverse()
还有。

怎么样

things.sort{it.dtCreated}
Collections.reverse(things)

寻找一些更有用的列表实用程序

您也可以使用things.first()和things.last()来提取列表的第一个和最后一个元素。

非常感谢,第一个对我来说非常有用,我以前尝试过。reverse()不适用于我的情况,但适用于其他情况。reverse(true)如果你以后还想循环它的话,它也在工作。
things.sort{-it.dtCreated.time}
things.sort{it.dtCreated}
Collections.reverse(things)