Querying array in the order it appears in Sanity Studio
8 replies
Last updated: Sep 16, 2022
A
Hello everyone I need help querying an array in the order it appears in the sanity Studio :
Inside my Page document, I have a pageBuilder array which contains sections.
Inside Sanity studio, I can order these section inside my pageBuilder.
However, when I query the pageBuilder using GROQ, I get an object with the section in alphabetical order.
How can I get the same order as it is displayed in the Sanity studio?
thanks
Inside my Page document, I have a pageBuilder array which contains sections.
Inside Sanity studio, I can order these section inside my pageBuilder.
However, when I query the pageBuilder using GROQ, I get an object with the section in alphabetical order.
How can I get the same order as it is displayed in the Sanity studio?
thanks
Sep 16, 2022, 4:02 PM
V
If I am not mistaken, your original query doesn't require itself to respect the "natural" order of the array. It is written alphabetically by your projections' key names and returned that way.
If you asked for the page builder array outright, directly, the returns should be in the order they appear in the studio.
That said, you obviously still want to treat each section differently; is it possible you could ask for all sections, and use
conditional selections to stipulate how they're handled internally if and when they come up?
Then, if need be for "corrections" after the fact, you could re-sort the indices on the front end.
If you asked for the page builder array outright, directly, the returns should be in the order they appear in the studio.
That said, you obviously still want to treat each section differently; is it possible you could ask for all sections, and use
conditional selections to stipulate how they're handled internally if and when they come up?
Then, if need be for "corrections" after the fact, you could re-sort the indices on the front end.
Sep 16, 2022, 4:13 PM
A
Hey thanks for your help, this is what I came up with following your advice :
Sep 16, 2022, 4:40 PM
A
I think that works š
Sep 16, 2022, 4:40 PM
V
Awesome, glad to hear it! You might get away with also using what they all share just the once, and only the truly different ones inside the conditions.
Like they all seem to have a _type and heading, you could just write that twice, then do the conditions and leave those two out of the conditions.
It looks like the hero returned way lower, though; when I just did an ordered array myself it preserves the order on my front end so I am not sure what's going on there. How does this new query open? Is it something along the lines of
?
Like they all seem to have a _type and heading, you could just write that twice, then do the conditions and leave those two out of the conditions.
It looks like the hero returned way lower, though; when I just did an ordered array myself it preserves the order on my front end so I am not sure what's going on there. How does this new query open? Is it something along the lines of
*[type=="page" && title=""homepage"][0] { pageBuilder[] { // Other inside things } }
Sep 16, 2022, 4:55 PM
A
alright thanks for the tip !
not sure to understand your question about the hero though
not sure to understand your question about the hero though
Sep 16, 2022, 5:03 PM
A
the hero query is a bit different because it is not a reference:
Sep 16, 2022, 5:03 PM
V
Oh yeah, I was just mistaken thinking it was first on your list and trying to figure out why it didn't lead the return. You're good! I tend to overthink sometimes (all the time).
Sep 16, 2022, 5:07 PM
A
all good, thanks for your help !
Sep 16, 2022, 5:53 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.