list Groovy/Grails:如何按 id 对对象列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19263836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Groovy/Grails : How to sort the list of objects by id
提问by monda
PublicTraining Class
公共培训班
class PublicTraining{
static hasMany = [trainingOrder: TrainingOrder]
}
and TrainingOrder Class
和 TrainingOrder 类
class TrainingOrder {
Date createdOn
static mapping = {
sort id:"asc"
}
}
if i want to get all the orders for training
如果我想获得所有培训订单
def orders = publicTrainingInstance.trainingOrder.sort()
println orders // [59,58] (id of orders)
which does not give sorted orders
不提供排序顺序
回答by Igor Artamonov
Default sort()
is useful for Comparable
object. If your class is not a Comparable
, use:
默认值sort()
对Comparable
对象很有用。如果您的课程不是Comparable
,请使用:
def orders = publicTrainingInstance.trainingOrder.sort { it.id }
That code will sort by using passed id.
该代码将使用传递的 id 进行排序。
See docs: http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort()
请参阅文档:http: //groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort()