Sorting collections

Sorting collections

When fetching multiple content items, you can define the order of returned records by using the sort argument. This can be particularly useful when working with large datasets or complex queries. It helps to quickly locate specific records in your project.

You can order results in ascending or descending order by specific system fields and some non-relational custom fields that you add to your model. More information can be found below.

Sorting by common attributes

It's possible to sort by the common attributes created, changed and published dates in ascending or descending order. The current values for sorting those fields are created_on_ASC, created_on_DESC, changed_on_ASC, changed_on_DESC, publish_on_ASC, publish_on_DESC.

query {
  Posts( sort : publish_on_DESC, locale : "en-US") {
    items {
        _id,
        title
    }
  }
}

Sorting by fields

You can sort content items by the content of all Text, Float, Integer and Date DateTime fields in either ascending or descending order. For example we've added an integer field order to the model. That creates two new sort options: order_ASC and order_DESC.

query {
  Posts( sort : order_ASC, locale : "en-US") {
    items {
        _id,
        title
    }
  }
}

Default ordering

If you don't pass an explicit order value the returned content items are ordered by publish_on_DESC. This means that the most recently published content item appear at the top of the list.

Was this article helpful?

We’d love to learn from your feedback