
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeWhen sorting in descending order using GROQ's order() function, null values appear first by default, which isn't what you want. To push those null values to the end (after the oldest date), you can use the coalesce() function to assign a fallback value specifically for sorting purposes.
Here's how to handle it:
*[_type == "yourType"] | order(coalesce(dateField, "1900-01-01") desc)The coalesce() function returns the first non-null value from its arguments. So when dateField is null, it uses "1900-01-01" (or any date older than your oldest real date) as the sort value. This effectively places null values at the bottom when sorting in descending order.
Important notes:
dateField values in your documents—it only affects the sort order-999999 as your fallbackFor example, if you're sorting blog posts by publish date:
*[_type == "post"] | order(coalesce(publishedAt, "1900-01-01") desc)This will give you posts ordered from newest to oldest, with unpublished posts (where publishedAt is null) appearing at the very end.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store