GROQ: Sort list by lowercase/case-insensitive in query?

3 replies
Last updated: Jul 1, 2021
Is there still no way to sort a list by lowercase, in GROQ? I’ve found some topics on this but they date back to 2018 and 2019. I have a list of companies where some are only uppercase and it messes with the order in which they should be presented. I would rather not do a second sorting in code.

*[_type == "company"] | order(name asc)
This returns everything in order but everything uppercase is < lowercase. So companies whose name is an abbreviation (with all capital letters) is placed in front of companies with an “actual name”.
AI Update

Good news! GROQ now supports case-insensitive sorting using the lower() function. According to the GROQ Query Cheat Sheet, you can use:

*[_type == "company"] | order(lower(name) asc)

This will convert all company names to lowercase for sorting purposes, so companies with all-caps abbreviations (like "ABC COMPANY") will be sorted alphabetically alongside companies with regular capitalization (like "Acme Corp"), rather than all uppercase names being placed at the beginning.

The lower() function works as an expression within the order() function, transforming the field value before sorting is applied. This solves the exact problem you're describing where uppercase letters sort before lowercase letters in standard alphabetical ordering.

So yes, this capability has been added to GROQ since those 2018-2019 discussions you found! You can now handle case-insensitive sorting directly in your query without needing to do additional sorting in your application code.

You can also combine this with multiple sort criteria if needed:

*[_type == "company"] | order(lower(name) asc, _createdAt desc)

This approach keeps your sorting logic in GROQ where it belongs, making your queries more efficient and your frontend code cleaner.

Show original thread
3 replies
Hi! You can use
order(lower(name))
.
Works great, thanks!
Super!

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?