Search code examples
grailsgroovy

how to order by data descending with findAll query using grails?


Here is my code:

Notification.all.findAll{it.actionTeamBy != null && it.user.id == params?.getLong('user_id') && it.status == true}

Thank in advance!


Solution

  • You are looking for standard Iterable sorting that is done after the findAll call.

    assert [3,4,2,7,4].sort() == [2, 3, 4, 4, 7]
    

    So that would translate into something like this

    Notification.all.findAll {
        it.actionTeamBy != null && it.status == true &&
                it.user.id == params?.getLong('user_id')
    }.sort { a, b ->
        // implement sorting mechanism here
        // b.id <=> a.id // could work for you
    }