TheBrain
TheBrain

Reputation: 543

How to order by more than one field in Grails?

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.

Upvotes: 44

Views: 40188

Answers (10)

Hates_
Hates_

Reputation: 68671

This old solution no longer works. Please see mattlary's answer below

You may have to write a custom finder in HQL or use the 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")
}

Upvotes: 8

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

def listCalendar (Calendar calendar) {
    respond CalendarData.where {
        calendar == calendar
    }.list().sort{ "$it.attraction.type?:' '$it.attraction.name" }
}

Upvotes: 0

gerrit-hntschl
gerrit-hntschl

Reputation: 61

MyDomain.findAll(sort: ['first': 'desc','last':'desc'])

works with grails-datastore-gorm:6.0.3

Upvotes: 6

mattlary
mattlary

Reputation: 999

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')
    }
}

Upvotes: 83

maq
maq

Reputation: 51

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 .

Upvotes: 5

chim
chim

Reputation: 8573

More complicated ordering criteria, (tested in 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

Upvotes: 5

Nakul
Nakul

Reputation: 41

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')

Upvotes: 4

Arnar B
Arnar B

Reputation: 111

This is quite old but helped me in finding a suitable solution. A "cleaner" code example by using withCriteria shortcut:

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

Upvotes: 11

Matthew Taylor
Matthew Taylor

Reputation: 3961

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.

Upvotes: 4

j pimmel
j pimmel

Reputation: 11637

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

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

Upvotes: 0

Related Questions