decap-cms icon indicating copy to clipboard operation
decap-cms copied to clipboard

Sorting with custom date format seems wrong

Open pmpinto opened this issue 5 years ago • 10 comments

Describe the bug I have a release-date field with a custom date_format set to DD-MM-YYYY, so something like:

name: 'release-date'
label: 'Release date'
widget: 'datetime'
date_format: 'DD-MM-YYYY'
format: 'DD-MM-YYYY'

Then I have release-date set under sortable_fields, which gets recognized, but doesn't quite work as expected.

Expected behavior I would expect the items to be listed considering the date_format and sorted by year first, then month, then day. Not the opposite as it seems to be happening.

Screenshots Screenshot 2020-11-26 at 17 10 22

Applicable Versions:

  • Netlify CMS version: 2.10.71
  • Git provider: GitHub
  • OS: macOS 11.0.1
  • Browser version: Brave v1.16.76
  • Node.JS version: 12.17.0

pmpinto avatar Nov 26 '20 17:11 pmpinto

@pmpinto @erezrokah I would like to work on this issue.

shashank-bairy avatar Nov 28 '20 08:11 shashank-bairy

Thanks @BA1RY, I've assigned you to the issue

erezrokah avatar Nov 30 '20 10:11 erezrokah

@erezrokah I looked into it and noticed that below code snippet is where sorting happens and any changes have to be made there. https://github.com/netlify/netlify-cms/blob/74d5569bd3daf7cb878435432d9d4545001e7f82/packages/netlify-cms-core/src/reducers/entries.ts#L413 Also I came across this old issue (https://github.com/netlify/netlify-cms/issues/3973) and got to know that dates by default/ISO format are saved as Date objects and dates with custom format are saved as strings. Keeping these things in mind, how do you want me to go about implementing the fix? Please correct me if I'm wrong in my understanding.

shashank-bairy avatar Nov 30 '20 16:11 shashank-bairy

Great research @BA1RY. I think that for sorting we would need to add a special case for dates. I'm guessing here, but looks like the bug is caused by us sorting the dates as strings (and not date objects) since they were saved as strings. We don't want to change the serialization/deserialization of those values, but do want to consider the origin type (date) while sorting. I'm not sure how to implement that yet though. Please let me know if that makes sense

erezrokah avatar Nov 30 '20 17:11 erezrokah

@erezrokah Understood. It makes sense. As you said the bug must be due to dates getting sorted as strings. Also is there a way to know if a particular field contains values for date/datetime and it's format (Ex: dd-mm-yyyy)? For example, if a field is named createdAt and it contains dates stored strings, unless I manually check if the incoming values are strings of date type and of particular format I wouldn't know.

shashank-bairy avatar Nov 30 '20 18:11 shashank-bairy

I think the only way to know for sure is based on the widget: date value of the field. You'd need to know the field anyway to extract the format and use if to covert the string back to a date instance.

erezrokah avatar Nov 30 '20 18:11 erezrokah

So how can I get to know widget type and format in the below method? I tried it and wasn't successful at it. https://github.com/netlify/netlify-cms/blob/74d5569bd3daf7cb878435432d9d4545001e7f82/packages/netlify-cms-core/src/reducers/entries.ts#L413

shashank-bairy avatar Nov 30 '20 18:11 shashank-bairy

I'll need to dig into it a bit more, I think we would need to save additional information for the sort fields

erezrokah avatar Dec 01 '20 11:12 erezrokah

So is what I'm seeing that the current workaround would be for me to convert all my dates to YYYY-MM-DD so that sorting as a string works as expected? I currently have everything as MM/DD/YYYY, but I could run a regex replace on my date fields if that will solve my sorting problem.

fitzage avatar Jan 24 '22 17:01 fitzage

So is what I'm seeing that the current workaround would be for me to convert all my dates to YYYY-MM-DD so that sorting as a string works as expected? I currently have everything as MM/DD/YYYY, but I could run a regex replace on my date fields if that will solve my sorting problem.

Yeah that seems to be the case. Don't mix and match the format. Either use ISO format or custom format (e.g. YYYY-MM-DD HH:mm:ss)

vhbui02 avatar Apr 29 '25 16:04 vhbui02