Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Sorting Featured Projects in Sanity Portfolio Schema

6 replies
Last updated: Dec 14, 2022
I have a quick question on sorting. I have a Featured Projects settings of my portfolio. I setup the schema to have the Project Settings Schema be an array of references to Projects. However, when listed it orders them by _id (I know this from referencing the v3 docs) even when I change the order if projects in the array. Is there a way to add a custom index value to each reference in the array when adding it? I could then to the order() function to order by that?
Dec 13, 2022, 11:43 PM
Hey
user F
! An array should return items in the order that they were added (or the order that you've manually sorted them into). That is, unless you've changed the sortable option to false. Is this not the behavior you're getting?
Dec 13, 2022, 11:47 PM
hey
user M
! Thanks for the quick response! I don't seem to be getting this behavior. This is my schema:

export default {
title: 'Featured Projects',
name: 'featuredProjects',
type: 'document',
fields: [
{
title: 'Featured Projects',
name: 'featuredProjects',
type: 'string',
hidden: true,
},
{
name: 'projects',
title: 'Featured Projects',
type: 'array',
of: [
{
type: 'reference',
to: [
{
type: 'projects',
},
],
options: {
disableNew: true,
},
},
],
validation: (
_Rule_: _any_) =>

_Rule_.max(4).warning('Only 4 Featured Projects Allowed'),
},
],
}
Dec 14, 2022, 12:09 AM
Tried it again: Should be listing the Pomodoro App first and then the Test App
Dec 14, 2022, 2:48 AM
user M
I think I know what is wrong. It's my query. If I query the _type == "featuredProjects" the result is in order as in the array. However, if I do a left join it lists them in order by _id. Is there a better way to do this other than a Left Join?
Dec 14, 2022, 4:17 AM
user M
I fixed it šŸ™‚

*[_type == "featuredProjects"]{
projects[]->{
"name": projectName,
"slug": slug.current,
excerpt,
gitHubUrl,
liveSiteUrl,
"image": image.asset._ref,
"tags": tags[]->{
name
}
}
}
Dec 14, 2022, 5:26 AM
Awesome! Glad you got it figured out!
Dec 14, 2022, 5:34 PM

Sanityā€“ build remarkable experiences at scale

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

Was this answer helpful?