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?
This works fine was just wondering is there a way to set a default insted of having the user select in manually
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
Unless I’m missing something
you should do the default ordering in the desk structure then
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
OK nice thanks fro the share!
you're welcome 🙂
Got it working thanks for clearing that up for me! This is my first Sanity project still trying to wrap my head around everything
Glad to help if you have more questions
this fix is for programmers. what about the end user? How do they sort the blog?
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" }],
    },
  ],
}
this is what the end users will see

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?