Order your results

You can use the sortBy argument to sort your query results in a particular order.


The sortBy argument

The sortBy argument orders the results by a specific field. These fields will be separated into two categories: Meta fields that are metadata included in all documents and custom fields that are specific to each Custom Type.

You can provide a sortBy argument for the generated fields in the schema. Each field will provide you with either an ASC (ascending order) or DESC (descending order) option.

Generated arguments for all sorting options

All the arguments available for the sortBy operation are generated based on the Custom Type's fields and the available meta data. You can see all the available arguments by hitting CTRL+SPACE to get the autocomplete options in your GraphiQL explorer.

Sort by a content field

When performing a query for a given document type, you can sort the results by the top-level fields in that type.
You can use any of the following valid fields:

  • Rich Text
  • Title
  • Key Text
  • Select
  • Date
  • Timestamp
  • Number

For example, here is how to get all the "product" documents sorted by a number field that has the API ID of "price" from highest to lowest (descending order).

Copy
query{
  allProducts(sortBy:price_DESC){
    edges{
      node{
        title
        price
      }
    }
  }
}

And here is an example that will sort all the documents of the type Author by a Key Text field with the API ID of "name" from A to Z (ascending order).

Copy
query{
  allAuthors(sortBy:name_ASC){
    edges{
      node{
        name
      }
    }
  }
}

Note that only top-level fields can be used with the sortBy argument, meaning that fields inside slices and/or groups are unavailable.

The sortBy argument cannot be used with custom API ID fields alone, they always have to be followed with either an ASC (ascending order) or DESC (descending order) option.

For example, if you were to use sortBy with a Rich Text field that has the API ID of "description", your argument options would be either: description_ASC or description_DESC.

Sort by publication date

It is possible to sort by first and last publication dates in either ascending or descending order for either.

First publication date

To sort by first publication date, simply use the sortBy argument with either meta_firstPublicationDate_ASC or meta_firstPublicationDate_DESC. This can be applied to any query.

Here is an example of how to get all "blog_post" documents sorted by the first publication date from newest to oldest (descending order).

Copy
query{
  allBlog_posts(sortBy:meta_firstPublicationDate_DESC){
    edges{
      node{
        title
        _meta{
          firstPublicationDate
        }
      }
    }
  }
}

Last publication date

To sort by last publication date, simply use the sortBy argument with either meta_lastPublicationDate_ASC or meta_lastPublicationDate_DESC. This can be applied to any query.

Here is an example on how to get all "blog_post" documents sorted by last publication date from oldest to newest (ascending order).

Copy
query{
  allBlog_posts(sortBy:meta_lastPublicationDate_ASC){
    edges{
      node{
        title
        _meta{
          lastPublicationDate
        }
      }
    }
  }
}