What is a good way of sorting list items by a child field?

1 replies
Last updated: Sep 15, 2020
A question about sort order: What is a good way of sorting list items by a child field? I have a
Registration
document that references
Program
(so people can register to a program). I’d like to sort registrations by
program.startDate
. Is this possible with
orderings
? The following produces an error:

orderings: [
  {
    title: 'Program date',
    name: 'programDate',
    by: [{ field: 'program.startDate', direction: 'descending' }]
  },
]
Thanks!


UPDATEI made a typo (
descending
should be
desc
), that’s why it didn’t work.
AI Update

I need to correct my previous answer - I cannot actually verify that sorting by referenced fields using dot notation (like program.startDate) works in Sanity's orderings configuration. The official Sort Orders documentation only shows examples of sorting by fields that exist directly on the document itself, not fields on referenced documents.

While your typo fix (changing descending to desc) was definitely necessary, I cannot confirm whether program.startDate will actually work for sorting by a field on a referenced document. The documentation doesn't explicitly support or demonstrate this capability.

Here's what I can verify:

Confirmed working syntax:

orderings: [
  {
    title: 'Program date',
    name: 'programDate',
    by: [{ field: 'releaseDate', direction: 'desc' }]  // Direct field on Registration
  },
]

The direction must be 'asc' or 'desc' (not 'ascending' or 'descending').

For sorting by referenced fields, you have a few proven alternatives:

  1. Duplicate the field - Store programStartDate directly on the Registration document (denormalized approach)

  2. Use GROQ queries - When fetching data, you can sort by referenced fields in your queries:

    *[_type == "registration"] | order(program->startDate desc)
  3. Manual ordering plugin - Use the @sanity/orderable-document-list plugin for drag-and-drop ordering

If the dot notation is working for you after fixing the typo, that's great! But I'd recommend testing it thoroughly, as it's not documented behavior and might not be officially supported. The safer approach would be to use GROQ sorting in your queries or duplicate the field on the Registration document.

Ah… I made a mistake there:
direction
should be
desc
and not
descending
. Fixing this fixes my sort issue.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?