43

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.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
TheBrain
  • 533
  • 1
  • 4
  • 11

10 Answers10

83

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')
    }
}
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
mattlary
  • 999
  • 6
  • 8
11

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')
    }
}
Arnar B
  • 111
  • 1
  • 2
8

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")
}
doelleri
  • 19,232
  • 5
  • 61
  • 65
Hates_
  • 66,613
  • 6
  • 32
  • 37
  • Not sure why it has been marked negative but the first approach Mydomain.find definitely works. probably he has some typo in the above but that is no reason for minus. +1 from me. – saurabh Dec 17 '12 at 12:39
  • 9
    Worked 5 years ago ;) Shame I can't delete it. – Hates_ Jan 04 '13 at 23:11
  • 5
    Oh how I dislike people downvoting something that used to work. – miek Jan 22 '13 at 13:41
  • org.hibernate.QueryException: could not resolve property: last,first of: MyDomain at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1616) – Abdennour TOUMI Oct 09 '13 at 08:50
6
MyDomain.findAll(sort: ['first': 'desc','last':'desc'])

works with grails-datastore-gorm:6.0.3

  • This solution worked for me with Grails 4.0.3 and GORM 7.0.4. You can also do something like `Person.findByUsername(username, [sort: ['lastName': 'asc', 'firstName': 'asc']])` – jnunderwood Dec 31 '20 at 19:31
5

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

chim
  • 8,407
  • 3
  • 52
  • 60
5

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 .

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
maq
  • 51
  • 1
  • 2
4

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')
mcabral
  • 3,524
  • 1
  • 25
  • 42
Nakul
  • 41
  • 1
4

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.

Matthew Taylor
  • 3,911
  • 4
  • 29
  • 33
0

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

j pimmel
  • 11,617
  • 6
  • 33
  • 43
0

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" }
}