I experienced the following issue: In a page collection with a header like in the code below, the items will not be ordered chronologically if the field 'header.event.start' is in date format.
title: 'collection'
content:
items: '@self.children'
order:
by: header.event.start
dir: desc
This is an example of an item
title: 'item1'
event:
start: '21-9-2017 20:00'
end: '21-9-2017 22:00'
I think that the date format in the custom field is not recognized and ordering by number is used. Using the american date format makes the items ordered by month first, but it is not properly recognized as a date.
The collection header can use a dateRange in combination with order to achieve chronological ordering that can be either descending or ascending.
title: 'collection'
content:
items: '@self.children'
dateRange:
start: today
field: header.event.start
order:
dir: asc
This works for me but is kind of hacky. The documentation of dateRange should be updated to show the ordering options.
@vitopepito Thanks for creating this. I'll look at it today. Ref: #274
@vitopepito I think your workaround may not be doing what you think. The order configuration without a by parameter will default to the directory name of the page. So what's happening in your case is that it's sorting by the page directory and not the dateRange.field. The dateRange config does not play in to the order at all. If you want to use the same field for both you have to restate it in order.by.
Now, there is a problem with using custom dates in the header as your sort field. The sorter does not know the type of value the custom field is (aside from array fields, which are imploded), so it falls back to treating the field as a string for sorting.
So the real issue here is how we can determine if the field in the order.by setting is a date or not. I'm thinking about possibilities, but happy to hear suggestions. There is a order.sort_flags setting but that is expected to be a PHP sanctioned sort flag.
My current thought is to make a new Utils method to regex check a string and see if it is in one of the allowed date formats from \Grav\Common\Utils::dateFormats. Any objections?
You are probably right about my workaround.
Of course it would be nice if custom fields are recognised as dates.
Another option would be to just use a date format of type YYYY-MM-DD hh:mm for the custom field. The string sorter would work and with a twig |date() filter it could be converted to any other format for front facing content.
Closing to due to inactivity
Hi, I have the same problem as the original author. I would like to sort collection by date in header.event.start using format dd-mm-yyyy.
Is it possible now, a year later?
Most helpful comment
Hi, I have the same problem as the original author. I would like to sort collection by date in header.event.start using format dd-mm-yyyy.
Is it possible now, a year later?