You can sort objects using queries arguments such as those found in the
list method:
def airports = Airport.list(sort:'name')
However, you can also declare the default sort order for a collection in the mapping:
class Airport {
…
static mapping = {
sort "name"
}
}
The above means that all collections of
Airport
s will be sorted by default by the airport name. If you also want to change the sort
order , use this syntax:
class Airport {
…
static mapping = {
sort name:"desc"
}
}
Finally, you can configure the sorting at the association level:
class Airport {
…
static hasMany = [flights:Flight] static mapping = {
flights sort:'number', order:'desc'
}
}
In this case, the
flights
collection will always be sorted in descending order of flight number.