Thomas Buckley
Thomas Buckley

Reputation: 6056

Order hql results by specific property value

I have a domain that contains a property called 'Status'. This property can contain 'A','I','P','Pv','R'.

I have the following query:

def list = Deal.findAll('from Deal as d')

How can I order the results so that rows with status of 'P' are always returned at the top of the result set? (I don't care in what order they come after that).

Upvotes: 3

Views: 354

Answers (2)

Tiago Farias
Tiago Farias

Reputation: 3407

You can use the sort method with a comparator:

def list = Deal.findAll('from Deal as d').sort({a,b-> (a.status== 'P' && b.status != 'P') ? 0 : 1 })

Upvotes: 1

Justin Piper
Justin Piper

Reputation: 3274

Will this do what you want? Normally I'd test it before answering but I don't have an easy way to do that atm.

def list = Deal.findAll('''from Deal as d order by case d.property when 'P' then 0 else 1 end''')

Upvotes: 3

Related Questions