Sort documents by string field in Sanity desk
You can sort and group documents in Sanity Studio's Structure Tool by using the Structure Builder API with custom document lists. Here's how to group your products by vendor:
You can create a custom structure that sorts documents by the vendor field using the .defaultOrdering() method on document lists. In your sanity.config.ts (or structure/index.ts), you can set up something like this:
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
export default defineConfig({
// ... other config
plugins: [
structureTool({
structure: (S) =>
S.list()
.title('Content')
.items([
S.listItem()
.title('Products by Vendor')
.child(
S.documentList()
.title('Products')
.filter('_type == "product"')
.defaultOrdering([{field: 'vendor', direction: 'asc'}])
),
// ... other items
])
})
]
})The key part is .defaultOrdering([{field: 'vendor', direction: 'asc'}]) which will sort all products alphabetically by their vendor name.
If you want to take it further and actually group products into separate lists by vendor (not just sort them), you can use the dynamically group list items with a GROQ filter approach to create separate list items for each vendor.
Alternatively, you can define sort orders directly in your product schema using the orderings property as shown in the Sort Orders documentation:
{
name: 'product',
type: 'document',
fields: [
{
name: 'vendor',
type: 'string',
title: 'Vendor'
},
// ... other fields
],
orderings: [
{
title: 'Vendor A-Z',
name: 'vendorAsc',
by: [{field: 'vendor', direction: 'asc'}]
},
{
title: 'Vendor Z-A',
name: 'vendorDesc',
by: [{field: 'vendor', direction: 'desc'}]
}
]
}This will give editors a dropdown option to sort by vendor when viewing the product list in Studio. The Structure Builder approach with defaultOrdering makes it the default sort, while the schema orderings approach gives users the flexibility to choose different sort options.
Show original thread10 replies
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.