> ## Documentation Index
> Fetch the complete documentation index at: https://www.nestjs-query.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Sorting

You can sort by one or more fields by using the `sorting` parameter.

The `sorting` parameter is an array where each item has the following options.

* `field!` - The name of the field to sort by.
* `direction!` - The direction to sort either `ASC` or `DESC`.
* `nulls?` - Optional field to set nulls sort order `NULLS_FIRST` or `NULLS_last`

In this example we sort by title descending.

> [tab] GraphQL
>
> ```graphql theme={null}
> {
>   todoItems(sorting: [{ field: title, direction: DESC }]) {
>     pageInfo {
>       hasNextPage
>       hasPreviousPage
>       startCursor
>       endCursor
>     }
>     edges {
>       node {
>         id
>         title
>         completed
>         created
>         updated
>       }
>       cursor
>     }
>   }
> }
> ```

> [tab] Response
>
> ```json theme={null}
> {
>   "data": {
>     "todoItems": {
>       "pageInfo": {
>         "hasNextPage": false,
>         "hasPreviousPage": false,
>         "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
>         "endCursor": "YXJyYXljb25uZWN0aW9uOjQ="
>       },
>       "edges": [
>         {
>           "node": {
>             "id": "1",
>             "title": "Create One Todo Item",
>             "completed": false,
>             "created": "2020-01-14T07:00:31.763Z",
>             "updated": "2020-01-14T07:00:31.763Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjA="
>         },
>         {
>           "node": {
>             "id": "5",
>             "title": "Create Many Todo Items - 4",
>             "completed": true,
>             "created": "2020-01-14T07:01:27.805Z",
>             "updated": "2020-01-14T07:01:27.805Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjE="
>         },
>         {
>           "node": {
>             "id": "4",
>             "title": "Create Many Todo Items - 3",
>             "completed": false,
>             "created": "2020-01-14T07:01:27.805Z",
>             "updated": "2020-01-14T07:01:27.805Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjI="
>         },
>         {
>           "node": {
>             "id": "3",
>             "title": "Create Many Todo Items - 2",
>             "completed": true,
>             "created": "2020-01-14T07:00:34.111Z",
>             "updated": "2020-01-14T07:00:34.111Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjM="
>         },
>         {
>           "node": {
>             "id": "2",
>             "title": "Create Many Todo Items - 1",
>             "completed": false,
>             "created": "2020-01-14T07:00:34.111Z",
>             "updated": "2020-01-14T07:00:34.111Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjQ="
>         }
>       ]
>     }
>   }
> }
> ```

In this example we sort by completed and title.

> [tab] GraphQL
>
> ```graphql theme={null}
> {
>   todoItems(sorting: [{ field: completed, direction: ASC }, { field: title, direction: DESC }]) {
>     pageInfo {
>       hasNextPage
>       hasPreviousPage
>       startCursor
>       endCursor
>     }
>     edges {
>       node {
>         id
>         title
>         completed
>         created
>         updated
>       }
>       cursor
>     }
>   }
> }
> ```

> [tab] Response
>
> ```json theme={null}
> {
>   "data": {
>     "todoItems": {
>       "pageInfo": {
>         "hasNextPage": false,
>         "hasPreviousPage": false,
>         "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
>         "endCursor": "YXJyYXljb25uZWN0aW9uOjQ="
>       },
>       "edges": [
>         {
>           "node": {
>             "id": "1",
>             "title": "Create One Todo Item",
>             "completed": false,
>             "created": "2020-01-14T07:00:31.763Z",
>             "updated": "2020-01-14T07:00:31.763Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjA="
>         },
>         {
>           "node": {
>             "id": "4",
>             "title": "Create Many Todo Items - 3",
>             "completed": false,
>             "created": "2020-01-14T07:01:27.805Z",
>             "updated": "2020-01-14T07:01:27.805Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjE="
>         },
>         {
>           "node": {
>             "id": "2",
>             "title": "Create Many Todo Items - 1",
>             "completed": false,
>             "created": "2020-01-14T07:00:34.111Z",
>             "updated": "2020-01-14T07:00:34.111Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjI="
>         },
>         {
>           "node": {
>             "id": "5",
>             "title": "Create Many Todo Items - 4",
>             "completed": true,
>             "created": "2020-01-14T07:01:27.805Z",
>             "updated": "2020-01-14T07:01:27.805Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjM="
>         },
>         {
>           "node": {
>             "id": "3",
>             "title": "Create Many Todo Items - 2",
>             "completed": true,
>             "created": "2020-01-14T07:00:34.111Z",
>             "updated": "2020-01-14T07:00:34.111Z"
>           },
>           "cursor": "YXJyYXljb25uZWN0aW9uOjQ="
>         }
>       ]
>     }
>   }
> }
> ```

## Setting a default sort

When querying the default is based on the persistence layer. You can override the default by using the QueryOptions decorator on your DTO.

You can find the documentation and an example in the [QueryOptions reference](https://www.nestjs-query.dev/graphql/dtos.md#default-sort).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.