如何在 grails 1.3.7 中对具有 JodaTime / DateTime 类型属性的域对象进行排序?
我正在制作一个小型活动日历,我想按开始时间对活动进行排序!
我在 grails 中使用 JodaTime 插件作为 startTime 属性。 ( http://www.grails.org/JodaTime+Plugin )
那么,我怎样才能用这个数据类型排序?这是行不通的:
def sortedEvents = events.asList().sort({ a, b -> a.startTime <=> b.startTime } as Comparator)
我希望你能帮助我!
谢谢, Whitenexx
/编辑/ 这是我获取事件的代码:
def getEventsNext(Location location) {
def events = location.events.findAll { it.endTime >= new DateTime() }
def sortedEvents = events.sort{it.startTime}
System.out.println(sortedEvents); //test
return sortedEvents
}
在 /event/list 操作中,g:sortableColumn 一切正常(按 startTime 排序):
I'm working on a small event calendar and i want to sort the events by start time!
I'm using JodaTime Plugin in grails for the startTime attribute. ( http://www.grails.org/JodaTime+Plugin )
So, how can i sort with this datatype? This does not work:
def sortedEvents = events.asList().sort({ a, b -> a.startTime <=> b.startTime } as Comparator)
I hope you can help me!
Thanks,
whitenexx
/EDIT/
This is the code where i'm getting the events:
def getEventsNext(Location location) {
def events = location.events.findAll { it.endTime >= new DateTime() }
def sortedEvents = events.sort{it.startTime}
System.out.println(sortedEvents); //test
return sortedEvents
}
In /event/list action everything works fine with g:sortableColumn (sorting by startTime):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
要反转排序顺序,请使用:
仅供参考,Groovy 将此
sort()
方法添加到Collection
中,以便您可以删除asList()
如果events
已经是Collection
,则来自上面的代码。Try this:
To reverse the sorting order, use:
FYI, Groovy adds this
sort()
method toCollection
so you can removeasList()
from the code above ifevents
is already aCollection
.尝试重写域类中的
compareTo
方法。例如,
编辑
像这样对你的事件进行排序:
或者按照@Don的建议,groovier的等效项
Try overriding the
compareTo
method in your domain classes.For example,
Edit
Sort your events like so:
Or as suggested by @Don, the groovier equivalent
尝试
JavaDoc for isAfterNow( )
Try
JavaDoc for isAfterNow()