Query array in same order as Sanity Studio, not alphabetical
Good news - GROQ actually preserves the array order from your document by default! If you're seeing items in alphabetical order, it's likely due to how you're projecting the data, not the array itself.
When you query an array field like pageBuilder, GROQ maintains the exact order that exists in your document. Here's what you need to check:
The correct way to preserve order:
*[_type == "page"]{
pageBuilder
}Or if you need to expand references within the array:
*[_type == "page"]{
pageBuilder[]{
_type,
_key,
// other fields from your sections
}
}Common mistake that causes alphabetical ordering:
If you're using object projection with property names, those properties will appear alphabetically in the result. For example:
*[_type == "page"]{
"hero": pageBuilder[_type == "hero"][0],
"features": pageBuilder[_type == "features"][0],
"cta": pageBuilder[_type == "cta"][0]
}This creates an object with keys in alphabetical order, not preserving your original array order.
To maintain order, keep the array as an array - don't destructure it into separate properties. The order() function is for sorting results by specific fields, but you shouldn't need it here since arrays are already ordered.
If you're still seeing unexpected ordering, double-check that you're actually querying the array field directly and not creating a new object structure in your projection. The order you see in Studio should match what GROQ returns when querying the array field directly.
Show original thread8 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.