Sorting reference field dropdown in Sanity
Sanity is a highly customizable headless CMS, where schemas are defined using code.
A field for referencing other documents can be defined like this:
fields: [
defineField({
title: 'Fruit',
name: 'fruit',
type: 'reference',
to: [{type: 'fruit'}],
}),
],
And looks like this:
...pretty nice. But it would look better if we could sort the fruit alphabetically. According to the documentation, it does not seem like that is possible, but way down in a github issue, there lies the solution.
Sorting is undocumented, but as easy as this.
fields: [
defineField({
title: 'Fruit',
name: 'fruit',
type: 'reference',
to: [{type: 'fruit'}],
options: {
sort: [{field: 'name', direction: 'asc'}],
},
}),
],
Finally sorted:
That's it!
Found this post helpful? Help keep this blog ad-free by buying me a coffee! ☕