Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting with custom date format seems wrong #4637

Open
pmpinto opened this issue Nov 26, 2020 · 9 comments
Open

Sorting with custom date format seems wrong #4637

pmpinto opened this issue Nov 26, 2020 · 9 comments
Assignees
Labels
area: ui/collection good first issue type: bug code to address defects in shipped code

Comments

@pmpinto
Copy link

pmpinto commented Nov 26, 2020

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 pmpinto added the type: bug code to address defects in shipped code label Nov 26, 2020
@shashank-bairy
Copy link
Contributor

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

@erezrokah
Copy link
Contributor

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

@shashank-bairy
Copy link
Contributor

@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 (#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.

@erezrokah
Copy link
Contributor

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

@shashank-bairy
Copy link
Contributor

shashank-bairy commented Nov 30, 2020

@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.

@erezrokah
Copy link
Contributor

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.

@shashank-bairy
Copy link
Contributor

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

@erezrokah
Copy link
Contributor

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

@fitzage
Copy link

fitzage commented Jan 24, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ui/collection good first issue type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

4 participants