Collection ordering doesnt work

I’m trying to order my news by publish_date. This is what I have so far:

{% set collection ={
    'items': {
        '@taxonomy': {
            'category': 'Blog',
            'tag': 'Spielbericht'
    'order': {
        'by': 'header.publish_date',
        'dir': 'asc'
    'limit': 6,
    'published': true
}) %}

The result is:


Apparently nothing is sorted here. What am i doing wrong?

1 Like

Interesting. I’d start by looking at your date format settings, although it looks like whichever format is being used (sensible or US ridiculous :P), they are out of order.

Could you please post here those settings, as well as how you are outputting your collection?

Also, is publish_date explicitly set on all of these pages? I ask because some of the fallbacks for dates are not intuitive in my opinion.

1 Like

I’ve controlled all pages for publish_date and the date value and it’s set for every single page. This is how a news page looks like:

title: 'Spielbericht #12'
firstTeamScore: 0
secondTeamScore: 0
        - Blog
        - Spielbericht
date: '05-11-2021 00:00'
publish_date: '05-11-2021 00:00'

This is the markup for my debug ouput:

<div class="mt-5 pt-5">
    {% for page in collection %}
        {{  page.header.publish_date|date('d.m.Y') }} - {{|date('d.m.Y') }} - {{  page.header.title|raw }}<br>
    {% endfor %}

And here the date configuration from user/config/system.yaml:

    default:                                     # The default date format Grav expects in the `date: ` field
    short: 'jS M Y'                              # Short date format
    long: 'F jS \a\t g:ia'                       # Long date format
    publish_dates: true  

I had a quick look into the Grav core code and found a resolved issue that gave me some clues. I still don’t fully understand how it is sorting this because even as a string, it’s not in order.

Try these:

  • In your collection definition, change to just “publish_date”. It’s not a documented option but does appear to be supported, and it sorts differently in the code.
  • Try the sort_flags option documented there, as it appears to be involved in the issue mentioned. No guidance given and not sure what value you would use in Twig, but try the ones linked like ‘SORT_REGULAR’ (as a string I guess??) etc. So:
'order': {
        'by': 'header.publish_date',
        'dir': 'asc',
        'sort_flags': 'SORT_.........'

Do you know much about debugging in Grav? By selectively dumping values into the debug console, you might be able to see where the problem is introduced.

Otherwise, you might get more satisfaction from someone on Discord. If you do, mention that you posted here and link to this, and also report back here what you find out. It should be valid to ask a question in a Github issue too (this might even be a bug), but it’s really a question of luck whether they get looked at or answered.

Hello @hughbris, thanks for effort to find a solution. I’ve tried using the sort_flags parameter without success. I think sorting a collection should not be that complex. Due to time constraints I will use the chronological sorting with date now.

Try changing this to 'by': 'publish_date', though, it’s quite simple and might work.

1 Like

@hughbris, I think you are right…

Without using “header.”, the publish_date is treated as a date. When using header.publish_date the value seems to be treated as a string.

Here are some ordering results:

By: header.publish_date, asc

01:01 02/02/2022
02:02 01/01/2022

The above result matches a string sort and not a date sort.

By: publish_date, asc

02:02 01/01/2022
01:01 02/02/2022

This is a correct sorting by date.