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 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?