[英]Sorting domain class properties with respect to another domain class properties
I've two domain classes 我有两个领域课程
1. CustomerInterest.groovy 1. CustomerInterest.groovy
static hasMany = [activities:Activity]
static belongsTo=[customer:Customer,projectProperty:ProjectProperty]
static mapping={
activities sort:'dateCreated',order:'desc'
}
2. Activity.groovy 2. Activity.groovy
Date dateCreated
static belongsTo = [customerInterest:CustomerInterest, employee:Employee]
In a controller i am doing this.. 在控制器中,我正在这样做。
def customerDetails(Customer customer)
{
def customerInterest=customer.customerInterests
render view:"customerDetails",model:[customerInterest:customerInterest]
}
customerDetails.gsp customerDetails.gsp
<g:each in="${customerInterest}" var="ci">
${ci}
</g:each>
** **
Question: I want to sort CustomerInterest on property dateCreated of Activity domain.
问题:我想对Activity域的属性dateCreated排序CustomerInterest 。
** **
Any help as soon as possible would be appreciated. 任何帮助将不胜感激。
尝试这个
def customerInterest=customer.customerInterests.sort { it.activities.dateCreated }
Instead of using the sort order in the mappings definition, you can use the old java Comparable
-interface: 您可以使用旧的Java
Comparable
-interface来代替在映射定义中使用排序顺序:
class Foo implements Comparable {
int compareTo(anotherObject) {
// complex logic returning -1 or 0 or 1
}
}
Then you can call listOfFoos.sort()
and it will sort it with the help of the compareTo-method. 然后,您可以调用
listOfFoos.sort()
,它将在compareTo-method的帮助下对其进行排序。
Beware that (equals) ==
will also use this method, so ensure that (only) identical objects will return 0. 注意(等于)
==
也将使用此方法,因此请确保(仅)相同的对象将返回0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.