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

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.

Categorized in