Unlock seamless workflows and faster delivery with our latest releases- get the details

Nested GROQ Query - How to Sort Categories in Alphabetical Order

2 replies
Last updated: Nov 10, 2021
Hey! I'm doing a nested query for categories under a blog post and was wondering how i could use groq to sort the returning categories by alphabetical order, or if i need to make some fancy projection... or I'm better off handling it client side.the current query, which returns the posts in the appropriate order:

*[_type == "post"] | order(publishedAt desc, _updatedAt desc) {
    ...,
    categories[] -> {
    "slug": slug.current,
    title
  }
}
I tried to do a projection inside the query but I'm still very new to groq and trying to get the hang of it
Nov 8, 2021, 11:14 PM
Hi, sure! Maybe something like:
*[_type == "post"] | order(publishedAt desc, _updatedAt desc) {
  ...,
  "categories": categories[] -> {
    "slug": slug.current,
    title
  } | order(lower(title))
}

Nov 8, 2021, 11:29 PM
🙏 thanks that worked!!! I appreciate it
Nov 10, 2021, 3:40 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.

Was this answer helpful?