-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 collections by a date field (e.g., YYYY-MM-DD) does not give expected results. #1199
Comments
To further demonstrate this, using the same blueprint as above, I modified the It does not seem to be sorted on any field in this case. |
I believe we are starting from this which calls this Taking a quick look though the various functions used to build and do the sort, I do not see anyplace where it would be setting a special type of sort flag or otherwise resetting the value; however, there is a lot of logic here so I may not be seeing anything yet. |
I am doing some further debugging and find that It is using a locale specific sorting:
If you do not have In my testing for
I have and my
and got:
I checked the PHP docs for I next tried setting the config option I checked the I still get:
It would appear that there is something in the collation rules that causes a problem for this, but I am not sure how to diagnose further. I think I may need to try to change something on my server (if possible) or if we could introduce a grav parameter to set the collation to a static value to handle these types of cases. |
I've been testing this for a while, if the date is not wrapped in quotes, all works fine. Dates are compared as dates. If instead the field is wrapped in quotes like in your example, strings are compared with To fix this either drop the quotes, or use SORT_NUMERIC, which is a constant that translates to
|
In my example, I do not create the entries by hand, but used the admin to create it. The admin uses the quoted strings. If the quotes are impacting it, then the admin should be updated not to use quotes if this will give a more consistent experience. The SORT_NATURAL fix in I even tried setting it explicitly in with I also tried some variations for good measure, as is: But none of The natural sort via In your testing, what is your collation? As noted above the target server where I need to run this (shared host) is set at I am wondering if this is perhaps something with this collation and my PHP version? I am going to run some additional testing to see if I can have the shared server run a different version of PHP. |
Something weird happened while I was writing a test script, it suddenly started to work in my test script, then in the real code with I see now that 1.1.9 updated 2 days ago and that |
I can confirm that |
I got |
Closing to due to inactivity |
I am trying to sort a collection based on a date field, which is defined in
YYYY-MM-DD
format. The sorting works on regular text fields, but it is acting odd on these date fields. The date fields are simply string so I would not expect special behavior.e..g, given:
We would expect Jan 1 to be sorted so it is before May 4.
Here is my blueprint at:
[siteroot]/user/blueprints/pages/event.yaml
Which works correctly in the admin:
I then created a template to show the list of events at:
[siteroot]/user/themes/antimatter/templates/eventlist.html.twig
The first sort
{% set events = eventsRaw.order('header.startdate', 'desc') %}
does not work as expected. It looks like instead it ignore that this is even a real value and is just sorts by title. The sort for{% set events = eventsRaw.order('header.description', 'asc') %}
works. I have a more complex example in my real project, I can sort on any field that is not a date - however, if I do have any fields which are in theYYYY-MM-DD
format, they will also not sort as expected.Here is the code at:
[siteroot]/user/pages/event-list/eventlist.md
Here are four content pages I am using in my testing:
At:
[siteroot]/user/pages/event-list/new-years-2015/event.md
At:
[siteroot]/user/pages/event-list/ground-hog-day/event.md
At:
[siteroot]/user/pages/event-list/off-to-the-races/event.md
At:
[siteroot]/user/pages/event-list//back-to-the-future/event.md
Here are screenshots of what is rendered when the event-list template is used:
In the first screenshot, you can see that it was NOT sorted using the date field, but instead seems to have ignored the request or reverted for some reason?
I am wondering if the date-type field get special handling somewhere in one of the called functions used for the sorting?
The text was updated successfully, but these errors were encountered: