Grails 按特定属性值对hql结果排序
我有一个域,其中包含一个名为“Status”的属性。 此属性可以包含“A”、“I”、“P”、“Pv”、“R” 我有以下疑问:Grails 按特定属性值对hql结果排序,grails,groovy,hql,Grails,Groovy,Hql,我有一个域,其中包含一个名为“Status”的属性。 此属性可以包含“A”、“I”、“P”、“Pv”、“R” 我有以下疑问: def list = Deal.findAll('from Deal as d') 如何对结果进行排序,使状态为“p”的行始终返回到结果集的顶部?(我不在乎他们之后的顺序。)这能满足你的要求吗?通常我会在回答之前测试一下,但我没有一个简单的方法 def list = Deal.findAll('''from Deal as d order by case d.prope
def list = Deal.findAll('from Deal as d')
如何对结果进行排序,使状态为“p”的行始终返回到结果集的顶部?(我不在乎他们之后的顺序。)这能满足你的要求吗?通常我会在回答之前测试一下,但我没有一个简单的方法
def list = Deal.findAll('''from Deal as d order by case d.property when 'P' then 0 else 1 end''')
您可以将排序方法与比较器一起使用:
def list = Deal.findAll('from Deal as d').sort({a,b-> (a.status== 'P' && b.status != 'P') ? 0 : 1 })
获取所有
其中d.property=='P'
,然后获取由d.property排序的所有其他,并在客户端合并它们,这会是一个问题吗?例如:def list=Deal.findAll('from Deal as d,其中d.prop==“P”);list.addAll(Deal.findAll('from Deal as d其中d.prop“P”order by d.prop))
(顺便说一下,这是元语法。根据您的需要进行调整。)def list=Deal.findAll('from Deal as d order by(d.status='P')创建映射并转换到比较器是不必要的。您只需传递闭包{a,b->(a.status='P'&b.status!='P'))?0:1}
到sort()
方法。