> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-compose/apis/graphql/sorting.md).

# Sorting

Explains how to sort GraphQL query results using single, multiple, and nested \`orderBy\` criteria.

You can sort GraphQL query results by specifying an `orderBy` argument.

The argument takes an array of sort criteria. Each item in the array specifies the type schema alias, field to sort on, and the direction. Use `ASC` for ascending order or `DESC` for descending order.

For example, to sort all products by the alphabetical order of their names, use the following query:

```graphql
query {
    products (orderBy: [{ product: { name: ASC }}]) {
        items {
            ... on Product {
                # requested fields
            }
        }
    }
}
```

When sorting by a Compose system field such as `id` or `variant`, the type schema alias should be omitted.

```graphql
query {
    products (orderBy: [{ id: DESC }]) {
        items {
            ... on Product {
                # requested fields
            }
        }
    }
}
```

## Sorting on Multiple Fields

You can specify multiple sort criteria. Criteria earlier in the array take precedence. Later sorts apply only when earlier values are equal.

For example, to order products alphabetically by category, then by name, use the following query:

```graphql
query {
    products (orderBy: [
        { product: { category: ASC }}
        { product: { name: ASC }}
    ])
    {
        items {
            ... on Product {
                # requested fields
            }
        }
    }
}
```

## Nested Sorting

You can also sort by nested properties using an object with the same nesting structure.

For example, to order books by the name of their author, use the following query:

```graphql
query {
    books (orderBy: [{
        book: {
            author: {
                name: ASC
            }
        }
    }])
    {
        items {
            ... on Book {
                # requested fields
            }
        }
    }
}
```

## Sorting Behaviour

You can sort on fields of type integer, number, string, and boolean. Arrays and objects cannot be sorted on directly.

Null values are placed first for ascending sorts and last for descending sorts.

A collection can contain items of more than one type schema. When you sort on a field of a specific type, results are grouped by type schema first. The groups follow the order in which the types appear in the `orderBy` array.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.umbraco.com/umbraco-compose/apis/graphql/sorting.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
