Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Help Sorting :(

Started by Muut Archive 11 years ago · 3 replies · 328 views
11 years ago

This should be simpler in my mind, I'm fried as usual.
Creating a list of images like this:

TWIG
{% set imgList =[] %}
{% for image in page.media.images %}
    {% set imgList = imgList|merge([image]) %}
{% endfor %}

Each image has an imagename.ext.meta.yaml file present such as this:

YAML
title: Mardis Gras Invitation
media: digital
intent: print
portfolio: true
description: Design for a benefit Mardi Gras party sponsoring The Human Race Theatre Company
date: 12/01/2015

I'd like to sort the imgList array by date, newest to oldest (descending?) when I'm outputting it like this:

TWIG
<ul>
{% for img in imgList %} 
    <li>date: {{ img.meta.get('date') }} title: {{ img.meta.get('title') }}</li>
{% endfor %}
</ul>

Please advise.

11 years ago

I would do this:

1) loop over image as you are doing, but insert them into an array with the key being the date.

2) sort the array by key

3) loop over the list again now in order by date.

10 years ago

@rhukster
I would do this:
1) loop over image as you are doing, but insert them into an array with the key being the date.
2) sort the array by key
3) loop over the list again now in order by date.
could you elaborate on this?
i am trying to achieve the exact same thing, but you lost me at "insert them into an array with the key being the date" and "sort the array by key"...
can i find this functionality documented somewhere?

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1368 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1125 9 years ago