🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Is there a way to set a default sort ordering in your documents?

15 replies
Last updated: Dec 2, 2020
Is there a way to set a default sort ordering in your documents? Say if you want the list of blog posts to sort by publish date by default without forcing to user to click the options and choose a defined sort option?
Oct 15, 2020, 5:28 PM
This works fine was just wondering is there a way to set a default insted of having the user select in manually
Oct 15, 2020, 5:30 PM
Oct 15, 2020, 5:32 PM
No I looked through there an it shows you how to create a sort option which appears in the dropdown screenshot but I didnt see a way to make a custom view default without clicking the options dropdown
Oct 15, 2020, 5:36 PM
Unless I’m missing something
Oct 15, 2020, 5:37 PM
you should do the default ordering in the desk structure then
Oct 15, 2020, 5:37 PM
Ah yeah I was wondering if thats where it should go. So if you have to set the order in the deskStructure file I guess
Oct 15, 2020, 5:38 PM
OK nice thanks fro the share!
Oct 15, 2020, 5:40 PM
you're welcome 🙂
Oct 15, 2020, 5:41 PM
Got it working thanks for clearing that up for me! This is my first Sanity project still trying to wrap my head around everything
Oct 15, 2020, 6:08 PM
Glad to help if you have more questions
Oct 15, 2020, 6:40 PM
this fix is for programmers. what about the end user? How do they sort the blog?
Dec 1, 2020, 7:12 PM
you can add sort options to the deskstructure like this:

S.documentList()
  .title("Blog")
  .filter('_type == "blog"')
  .menuItems([...S.documentTypeList("blog").getMenuItems()])
and in your blog schema

{
  name: "blog",
  title: "Blog",
  type: "document",
  ...,
  orderings: [
    {
      title: "Title",
      name: "title",
      by: [{ field: "title", direction: "asc" }],
    },
  ],
}
Dec 2, 2020, 6:28 AM
this is what the end users will see
Dec 2, 2020, 6:30 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Rendering nested block contentJan 18, 2021
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023
GraphQL query resolving references on a _raw field in GatsbyMay 17, 2022
How to populate a list of values in a Sanity schema using a separate fileSep 6, 2021
Handling dynamic meta titles and descriptions in SanityFeb 25, 2021
Error: unable to resolve image URL from source (undefined)Sep 11, 2022
Extracting referenced images from an object array in Sanity.ioMay 17, 2020

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle