////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

You can sort objects using query arguments such as those found in the <<ref-domain-classes-list,list>> method:

[source,java]
----
def airports = Airport.list(sort:'name')
----

However, you can also declare the default sort order for a collection in the mapping:

[source,java]
----
class Airport {
    ...
    static mapping = {
        sort "name"
    }
}
----

The above means that all collections of `Airport` instances will by default be sorted by the airport name. If you also want to change the sort _order_, use this syntax:

[source,java]
----
class Airport {
    ...
    static mapping = {
        sort name: "desc"
    }
}
----

Finally, you can configure sorting at the association level:

[source,java]
----
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.

WARNING: These mappings will not work for default unidirectional one-to-many or many-to-many relationships because they involve a join table. See <<ref-orgbrowse-grails-4089-this issue,this issue>> for more details. Consider using a `SortedSet` or queries with sort parameters to fetch the data you need.
