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.io – build remarkable experiences at scale

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