Sanity items displaying in wrong order on website

14 replies
Last updated: Sep 24, 2022
Hello Everyone, help
I have a terrible problem with Sanity, can anybody help me?!
when I add items in each sanity section in my desired order, when displayed on my website, they are totally scrambled and out of order , this is what I posted, and next is what is displayed on my website, items are not in order, WHYY?!!
AI Update

I understand your frustration! The good news is this is a common issue with a straightforward solution. When Sanity documents appear scrambled on your website, it's because you need to explicitly sort them in your GROQ query - Sanity doesn't automatically preserve the order you see in the Studio.

By default, Sanity returns documents in an unpredictable order (usually by internal ID or creation time), which is why they appear scrambled. Here's how to fix it:

The Solution: Use order() in Your Query

You need to add the order() function to your GROQ query. Here's the basic syntax:

*[_type == "yourType"] | order(fieldName asc)

Common Ordering Scenarios:

By date (newest first):

*[_type == "post"] | order(publishedAt desc)

By title (alphabetically):

*[_type == "product"] | order(title asc)

By custom order field: If you want manual control over order, add an orderRank field to your schema (type: number), then:

*[_type == "section"] | order(orderRank asc)

By multiple fields:

*[_type == "movie"] | order(releaseYear desc, title asc)

If You Want Drag-and-Drop Ordering

If you want to manually arrange items in the Studio and have that order reflected on your site, you should use the orderable document list plugin. This adds a special orderRank field that preserves your manual ordering.

The key takeaway: Sanity doesn't remember the order you see in Studio unless you explicitly query for a specific order. Add | order(yourField desc) to your query and your scrambled items will line up perfectly!

What field do you want to sort by? If you share your query, I can help you add the right ordering syntax.

Show original thread
14 replies
Do you order them in your query? The studio typically displays them in last updated order, but that’s not necessarily what your query does. You may need to be specific.
how can I change the order in my website?!! they dont change
I add them , but they dont show up in order that I added them
You can do that with the
order()
function in your query.
in react? how?!
for instance, I add my publications in order in Sanity , but they appear scrambled. in Sanity I add them so there should be a solution in Sanity , is that right?!
In your query. The groq query.
Show me your groq query. 🙂
const Publications = () => { const [publications, setPublications] = useState([]);

useEffect(() => {
const query = '*[_type == "publications"]';

client.fetch(query).then((data) => {
setPublications(data);
});
}, []);

return (
<>
<h2 className="head-text">Publications</h2>

{publications.map((publication) => (
<motion.div className="app__skills-exp-item">
<div>
<p>
<span className="p-text2">{publication.authors}</span>
<span className="p-text2">{publication.title}</span>
<span className="p-text3">{publication.journal}</span>
</p>
</div>
</motion.div>
))}

</>
);
};

export default AppWrap(
MotionWrap(Publications, "app__skills"),
"publications",
"app__whitebg"
);
Just the groq query was enough. 🙂
This is how you‘d order your data when querying it:
*[_type == "publications"] | order(_updatedAt desc)
you mean I add _updateAt desc ?!!
order(_updatedAt desc)

After a pipe, yes. Like I’ve shown. You can also check the docs: https://www.sanity.io/docs/query-cheat-sheet#b5aec96cf56c

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.

Was this answer helpful?