Ordering Celebs by their most recently updated facts in Sanity.io
12 replies
Last updated: Jun 10, 2022
M
Hi! I'd like to know how to order values by a referenced field.
I have the document type
I also posted this question on Stackoverflow to keep a record of the answer
https://stackoverflow.com/questions/72564416/sanity-groq-order-by-the-value-of-a-referenced-field
I have the document type
celeb. And the document type
fact. Each
factreferences a
celeb. I want to list
celebsordered by their most recently updated
facts. So I want to get a list of
celebswhere the
celebthat just had a
factadded about them would show up at the top.
I also posted this question on Stackoverflow to keep a record of the answer
https://stackoverflow.com/questions/72564416/sanity-groq-order-by-the-value-of-a-referenced-field
Jun 9, 2022, 5:37 PM
J
Hi!
I think something along the lines of this should work:
I think something along the lines of this should work:
*[_type == 'celeb']{ ..., "latestFact": *[_type == 'fact' && references(^._id)] | order(_updatedAt)[0]._updatedAt }| order(latestFact)
Jun 10, 2022, 2:09 PM
A
Hi! Something like:
This assumes that each
For each celeb, it finds the latest fact. At the end, it sorts all the celebs by the date.
*[_type == "celeb"] { _id, name, "latestFact": *[_type == "fact" && celeb._ref == ^._id] | order(_updatedAt desc)[0] } | order(latestFact._updatedAt desc)
facthas a field called
celebwhich is a ref to the celeb document.
For each celeb, it finds the latest fact. At the end, it sorts all the celebs by the date.
Jun 10, 2022, 2:09 PM
J
hah!
user L
Jun 10, 2022, 2:10 PM
A
Comment race condition 😁
Jun 10, 2022, 2:10 PM
A
The difference between these two answers is that Jørn’s will just fetch the latest timestamp, while mine will include the latest fact.
Jun 10, 2022, 2:13 PM
P
A slightly different approach if you ever needed a third one 😄 GROQ is flexible. This one doesn't require a sub-query, although it does reorder your facts. You can work around that if needed:
*[_type == "celeb"]{ facts[]-> | order(_updatedAt desc) } | order(facts[0]._updatedAt)
Jun 10, 2022, 2:26 PM
A
user M
That can be a good solution if celebs have a factsfield; I assumed they didn’t. Also note that this will fetch all facts for each celeb first, even though only the first one is needed to sort. A subquery like this:
*[_type == "fact" && celeb._ref == ^._id] | order(_updatedAt desc)[0]
Jun 10, 2022, 2:30 PM
P
Ah, good catch! I read it the other way around indeed: that a
celebreferences a
fact. Please disregard the above 🙂
Jun 10, 2022, 2:34 PM
S
poweranger
Jun 10, 2022, 3:10 PM
S
user S
any more questions 😅Jun 10, 2022, 3:11 PM
M
So much brain power went into answering my question! Thanks everyone, very appreciated!
Jun 10, 2022, 3:33 PM
M
posted the answer on stackoverflow for posterity https://stackoverflow.com/questions/72564416/sanity-groq-order-results-by-the-value-of-a-referenced-field/72577337#72577337
Jun 10, 2022, 4:26 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.