Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Order By Date not working in GROQ query

3 replies
Last updated: Feb 8, 2021
Can someone tell me why this query is not working properly? The order by date is not correct. The 'date' field is defined in article document as follows:
{
    name: 'date',
    title: 'Post Date',
    type: 'datetime',
    description: 'Date and time of publishing',
    validation: Rule => Rule.required()
}

Feb 8, 2021, 6:45 PM
Does using the
dateTime()
function help?

... | order(dateTime(date) desc) ...
Feb 8, 2021, 6:56 PM
Is this function from sanity or javascript? When I try to run it in the vision plugin I get:
No function dateTime() defined for arguments (null)
and on front-end is undefined. Can you explain more?
Feb 8, 2021, 7:19 PM
it's a GROQ function https://www.sanity.io/docs/groq-functions#datetime-af1b4fe06624
so.. you just wrap your date in it


*[...YOUR-FILTER..] | order(dateTime(date) asc) [0..15] {date}
Feb 8, 2021, 7:28 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?