I have a few array fields in my Sanity project that would work better if they were automatically sorted based on their values rather than having a user manually sort them.
I tried to create a custom component like this:
import type { ArrayOfObjectsInputProps } from 'sanity'
export function sortedArray(props: ArrayOfObjectsInputProps) {
return props.renderDefault({
...props, onChange: (_patch) => {
//@ts-ignore
props.value?.sort(props.schemaType.options.sortFunc)
}
})
}
With something like this in my schema:
import { sortedArray } from '...'
//...
{
//...
components: { input: sortedArray },
options: {
//@ts-ignore
sortFunc: (a, b) => { /* Sorting Algorithm */ },
}
But whatever I try, I can’t get it to work.