Articles sorted by publishedAt DESC return jumbled results with wrong order

17 replies
Last updated: Feb 9, 2021
When fetching articles sorted by
publishedAt
date ordered DESC, results are somewhat jumbled with some of the articles that should be at the end of the list are being inserted at the beginning. The results in the screenshot start with the latest articles: 2021, then 2019 then 2021 again.
AI Update

This is a common issue when sorting by dates in GROQ - it typically happens when some documents have null or missing publishedAt values. Here's what's likely happening and how to fix it:

The Problem

When you use order(publishedAt desc), documents with null or undefined publishedAt values can appear at unpredictable positions in your results, causing the "jumbled" ordering you're seeing where 2021 articles appear, then 2019, then 2021 again.

The Solution

Filter out null values before sorting:

*[_type == "article" && defined(publishedAt)] | order(publishedAt desc)

The defined() function checks that the field exists and has a value, ensuring you only sort documents that actually have a publishedAt date.

Alternative Approaches

1. Use coalesce() to provide a fallback date:

*[_type == "article"] | order(coalesce(publishedAt, _createdAt) desc)

This will use _createdAt as a fallback when publishedAt is missing, so all articles can still be included in your results.

2. Separate published and unpublished articles:

{
  "published": *[_type == "article" && defined(publishedAt)] | order(publishedAt desc),
  "drafts": *[_type == "article" && !defined(publishedAt)] | order(_createdAt desc)
}

This gives you explicit control over how each group is handled.

Additional Tips

  • Always check your documents in the Vision plugin or Studio to verify which ones have missing publishedAt values
  • Consider making publishedAt a required field in your schema to prevent this issue going forward
  • Remember that GROQ's default ordering (when no explicit order is specified) is ascending by _id, which can also seem random if you're expecting chronological order

The most common fix is simply adding && defined(publishedAt) to your filter - this should give you clean, properly ordered results!

Show original thread
17 replies
Thanks for reporting, Alex! Could you try wrapping
publishedAt
in
dateTime()
to see if that makes a difference, even if it slows down the query?
Thanks for the quick response but which
publishedAt
do I wrap with
dateTime()
? Not sure where to put it.
Apologies, I assumed you were using GROQ here, which doesn't make sense given the channel name and screenshots you shared 😉 In that case, it'd have been like this:
order(dateTime(publishedAt) desc)
. Not here though.
We'll try to reproduce the issue on our end.
I see. I hope this get resolved soon. It's breaking the pagination of our site.
Could you add the document IDs to the query to verify that these are all published documents, including the ones that wrongly show up in the list?
Also, are you seeing correct results when ordering by
_createdAt
or
_updatedAt
?
Here with
_id

Yes, results seem to be accurate when ordered by
_createdAt
.
Interestingly, the articles jumbled in seems to be the ones I migrated from Wordpress with their own custom
_id
.
But that shouldn't affect the order, right?
Woops, wrong screenshots.
That's odd indeed. It shouldn't affect the order, but it does stand out that these have timestamps ending on 000Z, which perhaps were set slightly differently even if the distinction between the two doesn't surface in these results.
Could you compare an imported article with one of the others and check if their
publishedAt
values have identical formats when inspecting their raw JSON in the studio?
Here.
Some of the imported articles'
publishedAt
were manually set using Sanity's built-in datepicker.
You can see the jumbled results here https://opinyon.net/sections/opinion and here
http://opinyon.net/sections/entertainment
Everything was fine yesterday though. The imported articles were imported 2 weeks ago and we haven't made any changes with our queries since then.
Thanks for the additional details Alex. I can confirm that this is a regression on our side, and the back-end team are looking into it. Started late yesterday/early today. Will keep you updated here.
I see. I guess I'll sort by
_createdAt
at the moment. Thank you for help.
Hi again Alex, our back-end team just pushed a fix to production that should resolve these ordering issues on datetime fields. Please let me know if ordering by
publishedAt
now works properly.
Hi Peter, it's now working fine again. Thank you for the swift fix!

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?