Sort array by field

Hi guys,

again I have a question… In my press_releases.md I have an array defined in frontmatter.

press_items:
   -
      title: 'xxx'
      date: '01.09.2016'
      description: 'yyyy'
   -
      title: 'zzz'
      date: '26.09.2016'
      description: 'vvvv'

Now I would like to loop over them sorted by date. How can I achieve this?

Thanks in advance!

The best way is to just put them in your YAML in date order. Then you don’t have to do anything programmatically. In PHP, a google search returns something along these lines. I don’t believe twig can natively do this, but there are extensions that claim to do so.

Sorry. Not sure what happened to my text at the end there. It should read "but there are some extensions that claim to do so. The link is good. Now, I haven’t used twig extensions before, so I’m not certain how to incorporate them into Grav. That’s something I need to look into.

Good news. According to the Grav source code, it looks like a sort_by_keyfilter is available. Give it a shot and let us know if it worked.

In your case, though, it won’t work as you expect because of your date formats. You’ll have to convert them to ISO or epoch format for the filter to work properly.