Studio

Sort orders

Manage how Sanity Studio sorts documents.

When displaying a collection of documents, it's useful to be able to sort the collection by different fields. You do this by specifying an orderings property in the schema:

import {defineField, defineType} from 'sanity'

export const movie = defineType({
  name: 'movie',
  type: 'document',
  fields: [
    defineField({
      title: 'Title',
      name: 'title',
      type: 'string'
    }),
    defineField({
      title: 'Release Date',
      name: 'releaseDate',
      type: 'date'
    }),
    defineField({
      title: 'Popularity',
      name: 'popularity',
      type: 'number'
    })
  ],
  orderings: [
    {
      title: 'Release Date, New',
      name: 'releaseDateDesc',
      by: [
        {field: 'releaseDate', direction: 'desc'}
      ]
    },
    {
      title: 'Release Date, Old',
      name: 'releaseDateAsc',
      by: [
        {field: 'releaseDate', direction: 'asc'}
      ]
    },
    {
      title: 'Popularity',
      name: 'popularityDesc',
      by: [
        {field: 'popularity', direction: 'desc'}
      ]
    }
  ]
})

The orderings array defines the ways editors can order a list of movies. In the studio, each object in orderings becomes its own option in the document list's sort menu. For the movie type, the options are Sort by Release Date, New, Sort by Release Date, Old, and Sort by Popularity.

Ordering options

Each object in orderings takes these properties:

  • title: the label shown in the sort menu.
  • name: an identifier for the ordering.
  • by: an array of sort items. Each item takes a field to sort by and a direction of asc or desc. When by holds more than one item, each later item sorts documents that tie on the items before it.
  • i18n: an optional translation for title, given as a key and namespace from a locale bundle, for example i18n: {title: {key: 'sort.release-date-new', ns: 'movies'}}. The studio shows title when it finds no translation.

Default sort orders

If no sort orders are defined, the studio guesses which fields make sense to sort by.

It picks fields as follows:

  • If the document type has string fields named title, name, label, heading, header, caption, or description, the studio adds a sort option for each of them.
  • If the type has none of these fields, the studio generates sort options for all fields of primitive types, that is, fields of type string, number, or boolean.

If you specify your own orderings, the studio skips these default heuristics. Whether or not you define orderings, the sort menu always also includes Sort by Last Edited and Sort by Created. In the default document type lists, Sort by Last Edited is the initial sort order.

Null ordering

You can control where null or undefined values appear in the sort order with the nulls option. This option requires sanity 5.17.0 or later. The defaults follow the total comparison rules in the GROQ specification.

By default, null values sort as follows:

  • desc -> nulls first
  • asc -> nulls last
import {defineField, defineType} from 'sanity'

export const book = defineType({
  name: 'book',
  type: 'document',
  fields: [
    defineField({
      name: 'publicationYear',
      type: 'number',
    }),
    // ...
  ],
  orderings: [
    {
      title: 'Publication year',
      name: 'publicationYear',
      by: [
        {
          field: 'publicationYear',
          direction: 'desc',
          nulls: 'last' // or 'first'
        }
      ],
    },
  ],
})

Overriding the default null ordering is experimental. It may have performance implications and slow down loading for document types with many documents.

Order by reference fields

The Structure Tool automatically dereferences references accessed through dot notation in orderings. This differs from how GROQ dereferencing works, which uses the -> syntax. You can sort by a field on a referenced document using dot notation, for example, author.lastName where author is a reference field. This behavior is non-standard compared to GROQ and is handled automatically by the studio.

import {defineField, defineType} from 'sanity'

export const article = defineType({
  name: 'article',
  type: 'document',
  fields: [
    defineField({
      name: 'title',
      type: 'string',
      title: 'Title',
    }),
    defineField({
      name: 'author',
      type: 'reference',
      to: [{type: 'author'}],
      title: 'Author',
    }),
  ],
  orderings: [
    {
      title: 'Author last name, A-Z',
      name: 'authorLastNameAsc',
      by: [
        {field: 'author.lastName', direction: 'asc'},
      ],
    },
  ],
})

Order by array items

You can order by a specific item in an array field using bracket notation. For example, tags[0] sorts by the first element of a tags string array. This lets you create orderings based on the leading value in an ordered list. Array index access in orderings requires sanity 5.21.0 or later.

import {defineField, defineType} from 'sanity'

export const post = defineType({
  name: 'post',
  type: 'document',
  fields: [
    defineField({
      name: 'title',
      type: 'string',
      title: 'Title',
    }),
    defineField({
      name: 'tags',
      type: 'array',
      of: [{type: 'string'}],
      title: 'Tags',
    }),
  ],
  orderings: [
    {
      title: 'First tag, A-Z',
      name: 'firstTagAsc',
      by: [
        {field: 'tags[0]', direction: 'asc'},
      ],
    },
  ],
})

Was this page helpful?

Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change site theme