Is there a way to get a list ordered by two fields, say last and first names?
有没有办法获得由两个字段排序的列表,比如姓和名?
I know .listOrderByLastAndFirst
and .list(sort:'last, first')
won't work.
我知道.listOrderByLastAndFirst和.list(sort:'last,first')将不起作用。
10 个解决方案
#1
8
This old solution no longer works. Please see mattlary's answer below
这个旧解决方案不再有效。请参阅下面的mattlary的答案
You may have to write a custom finder in HQL or use the Criteria Builder.
您可能必须在HQL中编写自定义查找程序或使用Criteria Builder。
MyDomain.find("from Domain as d order by last,first desc")
Or
def c = MyDomain.createCriteria()
def results = c.list {
order("last,first", "desc")
}
#2
81
Hates_ criteria answer didn't seem to work for me; putting "last,first"
in order will only cause exceptions saying, "Property 'last,first' not found"
. To order on two fields, you can do the following:
Hates_标准答案对我来说似乎不起作用;将“最后,第一”按顺序排列只会导致异常,即“财产最后,先找不到”。要在两个字段上订购,您可以执行以下操作:
def c = MyDomain.createCriteria()
def results = c.list {
and{
order('last','desc')
order('first','desc')
}
}
#3
9
This is quite old but helped me in finding a suitable solution. A "cleaner" code example by using withCriteria shortcut:
这已经很老了,但帮助我找到了合适的解决方案。使用withCriteria快捷方式的“清洁”代码示例:
def c = MyDomain.withCriteria {
and {
order('last', 'desc')
order('first', 'desc')
}
}
#4
5
More complicated ordering criteria, (tested in Grails 2.1.0)
更复杂的订购标准,(在Grails 2.1.0中测试)
def c = MyDomain.withCriteria {
property {
order('last', 'desc')
}
order('first', 'desc')
}
sorts first by MyDomain.property.last then by MyDomain.first
首先通过MyDomain.property.last然后通过MyDomain.first排序
#5
4
This query is working on the basis of first field. When the first field is blank then it is shorted by the second field.
此查询基于第一个字段进行工作。当第一个字段为空时,它会被第二个字段短接。
order('last','desc')
order('first','desc')
#6
3
I think a criteria is the best bet, but you did the right thing by attempting a finder first. When retrieving domain objects from GORM, the right order to make the attempt is: dynamic finder, criteria, HQL.
我认为一个标准是最好的选择,但你通过首先尝试查找器做了正确的事情。从GORM检索域对象时,进行尝试的正确顺序是:动态查找器,条件,HQL。
#7
3
you can do this
你可以这样做
def results=MyDomain.findAll([sort:"last",order:'desc'],[sort:"first",order:'desc']);
this line of code will first sort results from domain class "MyDomain" first by last name and then by first name of the person .
这行代码将首先按照姓氏对域类“MyDomain”的结果进行排序,然后按人员的名字对结果进行排序。
#8
2
MyDomain.findAll(sort: ['first': 'desc','last':'desc'])
works with grails-datastore-gorm:6.0.3
使用grails-datastore-gorm:6.0.3
#9
0
If you were sorting lists on the contents of their items, you would need to implement a comparator which would have some smarts to enable to you decide the sort order based on multiple properties.
如果您要对其项目内容进行排序,则需要实现一个比较器,该比较器具有一些智能,可以根据多个属性决定排序顺序。
Some examples of Groovy-style comparators are shown here
这里显示了Groovy式比较器的一些示例
However if the list you are sorting is being returned from a database query, you would be better off sorting it using a CrteriaQuery and sorts on that
但是,如果正在从数据库查询返回您正在排序的列表,那么最好使用CrteriaQuery对其进行排序并对其进行排序
#10
0
I has the same problem. Since my list is not so big, I use groovy sort, as I want to sort on fields of linked domain: CalendarData -> Attraction
我有同样的问题。由于我的列表不是那么大,我使用groovy排序,因为我想对链接域的字段进行排序:CalendarData - > Attraction
def listCalendar (Calendar calendar) {
respond CalendarData.where {
calendar == calendar
}.list().sort{ "$it.attraction.type?:' '$it.attraction.name" }
}
#1
8
This old solution no longer works. Please see mattlary's answer below
这个旧解决方案不再有效。请参阅下面的mattlary的答案
You may have to write a custom finder in HQL or use the Criteria Builder.
您可能必须在HQL中编写自定义查找程序或使用Criteria Builder。
MyDomain.find("from Domain as d order by last,first desc")
Or
def c = MyDomain.createCriteria()
def results = c.list {
order("last,first", "desc")
}
#2
81
Hates_ criteria answer didn't seem to work for me; putting "last,first"
in order will only cause exceptions saying, "Property 'last,first' not found"
. To order on two fields, you can do the following:
Hates_标准答案对我来说似乎不起作用;将“最后,第一”按顺序排列只会导致异常,即“财产最后,先找不到”。要在两个字段上订购,您可以执行以下操作:
def c = MyDomain.createCriteria()
def results = c.list {
and{
order('last','desc')
order('first','desc')
}
}
#3
9
This is quite old but helped me in finding a suitable solution. A "cleaner" code example by using withCriteria shortcut:
这已经很老了,但帮助我找到了合适的解决方案。使用withCriteria快捷方式的“清洁”代码示例:
def c = MyDomain.withCriteria {
and {
order('last', 'desc')
order('first', 'desc')
}
}
#4
5
More complicated ordering criteria, (tested in Grails 2.1.0)
更复杂的订购标准,(在Grails 2.1.0中测试)
def c = MyDomain.withCriteria {
property {
order('last', 'desc')
}
order('first', 'desc')
}
sorts first by MyDomain.property.last then by MyDomain.first
首先通过MyDomain.property.last然后通过MyDomain.first排序
#5
4
This query is working on the basis of first field. When the first field is blank then it is shorted by the second field.
此查询基于第一个字段进行工作。当第一个字段为空时,它会被第二个字段短接。
order('last','desc')
order('first','desc')
#6
3
I think a criteria is the best bet, but you did the right thing by attempting a finder first. When retrieving domain objects from GORM, the right order to make the attempt is: dynamic finder, criteria, HQL.
我认为一个标准是最好的选择,但你通过首先尝试查找器做了正确的事情。从GORM检索域对象时,进行尝试的正确顺序是:动态查找器,条件,HQL。
#7
3
you can do this
你可以这样做
def results=MyDomain.findAll([sort:"last",order:'desc'],[sort:"first",order:'desc']);
this line of code will first sort results from domain class "MyDomain" first by last name and then by first name of the person .
这行代码将首先按照姓氏对域类“MyDomain”的结果进行排序,然后按人员的名字对结果进行排序。
#8
2
MyDomain.findAll(sort: ['first': 'desc','last':'desc'])
works with grails-datastore-gorm:6.0.3
使用grails-datastore-gorm:6.0.3
#9
0
If you were sorting lists on the contents of their items, you would need to implement a comparator which would have some smarts to enable to you decide the sort order based on multiple properties.
如果您要对其项目内容进行排序,则需要实现一个比较器,该比较器具有一些智能,可以根据多个属性决定排序顺序。
Some examples of Groovy-style comparators are shown here
这里显示了Groovy式比较器的一些示例
However if the list you are sorting is being returned from a database query, you would be better off sorting it using a CrteriaQuery and sorts on that
但是,如果正在从数据库查询返回您正在排序的列表,那么最好使用CrteriaQuery对其进行排序并对其进行排序
#10
0
I has the same problem. Since my list is not so big, I use groovy sort, as I want to sort on fields of linked domain: CalendarData -> Attraction
我有同样的问题。由于我的列表不是那么大,我使用groovy排序,因为我想对链接域的字段进行排序:CalendarData - > Attraction
def listCalendar (Calendar calendar) {
respond CalendarData.where {
calendar == calendar
}.list().sort{ "$it.attraction.type?:' '$it.attraction.name" }
}