How to order by more than one field in Grails?

前端 未结 10 1960
萌比男神i
萌比男神i 2020-12-13 09:03

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

相关标签:
10条回答
  • 2020-12-13 09:42

    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')
        }
    }
    
    0 讨论(0)
  • 2020-12-13 09:42
    MyDomain.findAll(sort: ['first': 'desc','last':'desc'])
    

    works with grails-datastore-gorm:6.0.3

    0 讨论(0)
  • 2020-12-13 09:45

    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')
        }
    }
    
    0 讨论(0)
  • 2020-12-13 09:45

    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" }
    }
    
    0 讨论(0)
提交回复
热议问题