////
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.
////
[[advancedGORMFeatures-defaultSortOrder]]
== Default Sort Order

You can configure a default sort order for `list()` and `findAll*` queries at the domain class level using the `mapping` block:

[source,groovy]
----
class Book {
    String title
    Date dateCreated

    static mapping = {
        sort 'title'            // <1>
    }
}
----
<1> All `Book.list()` calls will return results sorted by `title` in ascending order by default.

=== Sort Direction

[source,groovy]
----
static mapping = {
    sort title: 'desc'          // <1>
}
----
<1> Sort by `title` descending.

=== Default Sort on Associations

You can also define a default sort order on a `hasMany` collection:

[source,groovy]
----
class Author {
    static hasMany = [books: Book]
    static mapping = {
        books sort: 'title', order: 'asc'
    }
}
----

NOTE: The default sort order in `mapping` applies to queries that do not specify their own `order`. Any query that specifies `sort` or `order` explicitly will override the default.
