Reputation: 165
I have been using the following code for sorting a domain's results
vpisort.sort{it.price}
I was wondering If i could use something like
vpisort.sort{it.price-it?.discount}
Upvotes: 2
Views: 111
Reputation: 11012
hm. This is not a grails, but a groovy question. With this in mind, the solution is simple:
open the groovy console and give your line a try. Or check out these examples:
http://groovy.codehaus.org/JN1015-Collections
there is also another question like this one on SO:
Groovy list.sort by first, second then third elements
hope that helps!
Update: If you want to have a default sort order for your domain class, take a look at this GORM feature: http://grails.org/doc/latest/guide/single.html#ormdsl (5.5.3 Default Sort Order)
Upvotes: 1
Reputation: 3723
I've found this post. You can try using this in your case:
vpisort.sort { a, b -> a.price <=> b.price ?: a?.discount <=> b?.discount }
It comapres price first and then, only if a and b are equal by price and result is zero, using Elvis operator it compares a and by by discount.
Upvotes: 5