sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.23k stars 425 forks source link

Date sort order not working #6355

Open j-palindrome opened 6 months ago

j-palindrome commented 6 months ago

If you find a security vulnerability, do NOT open an issue. Email security@sanity.io instead.

Describe the bug

Sort order for date fields sorts in a seemingly random order.

To Reproduce

  1. Create a document schema:
import {defineField, defineType} from 'sanity'

const work = defineType({
  name: 'work',
  title: 'Work',
  type: 'document',
  fields: [
    defineField({
      name: 'releaseDate',
      type: 'date',
      validation: (rule) => rule.required(),
    }),
  ],
  orderings: [
    {
      title: 'Most Recent',
      name: 'releaseDateDesc',
      by: [{field: 'releaseDate', direction: 'desc'}],
    },
  ],
})

export default work
  1. Select the sort ordering, it will not sort correctly.

Expected behavior

Descending sort order for dates in YYYY-MM-DD format.

Screenshots

Screenshot 2024-04-13 at 12 14 47 PM

This collection is sorted like:

  1. 2022-11-01
  2. 2023-04-01
  3. 2021-11-01
  4. 2022-03-01

Which versions of Sanity are you using?

{
  npm: '9.8.0',
  node: '20.5.1',
  acorn: '8.10.0',
  ada: '2.5.1',
  ares: '1.19.1',
  base64: '0.5.0',
  brotli: '1.0.9',
  cjs_module_lexer: '1.2.2',
  cldr: '43.1',
  icu: '73.2',
  llhttp: '8.1.1',
  modules: '115',
  napi: '9',
  nghttp2: '1.55.1',
  nghttp3: '0.7.0',
  ngtcp2: '0.8.1',
  openssl: '3.0.10+quic',
  simdutf: '3.2.14',
  tz: '2023c',
  undici: '5.22.1',
  unicode: '15.0',
  uv: '1.46.0',
  uvwasi: '0.0.18',
  v8: '11.3.244.8-node.10',
  zlib: '1.2.13.1-motley'
}

What operating system are you using?

MacOS Sonoma 14.2.1

Which versions of Node.js / npm are you running?

Run npm -v && node -v in the terminal and copy-paste the result here.

9.8.0
v20.5.1
cngonzalez commented 5 months ago

Hi @j-palindrome -- thank you for reporting. I was able to see some of the same behavior that you're mentioning, and I agree that it's a bit unexpected and probably not the best user experience.

In my case, the issue was caused by two things --

  1. Fields with a null or undefined datetime value get sorted first
  2. The sort does not always privilege the draft document. So if I had a published document like:
    {
       "_id": "my-exhibition-document",
       "_type": "exhibition",
       "startDate": "2024-05-16T15:25:39.429Z"
    } 

but I had a working draft document where I updated that date, like:

    {
       "_id": "drafts.my-exhibition-document",
       "_type": "exhibition",
       "startDate": "2024-02-16T15:25:39.429Z"
    } 

The document would show up higher than it should have, based on what I was looking at.

In your case, do you have discrepancies between published and draft documents, or have undefined datetime fields?

Again, this is behavior that should be rectified and I am putting in a ticket for it, but I just wanted to confirm with you that we have accurately captured what's going on, and what we need to work on.

Thanks again for reporting, and for your help!