How to order by more than one field in Grails?

TheBrain picture TheBrain · Nov 28, 2008 · Viewed 37.3k times · Source

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.

Answer

mattlary picture mattlary · Oct 27, 2009

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:

def c = MyDomain.createCriteria()
def results = c.list {
    and{
       order('last','desc')
       order('first','desc')
    }
}