How to reverse collection list?

Hello,

I use this

{% for project in site.projects %}
  <h3><a href="{{ project.url }}">{{ project.title }}</a><h3>
{% endfor %}

to generate a list of project titles from the my_collections/_projects folder.

In the _config.yml I have this:

collections:
  projects:
    permalink: /:collection/:title
    output: true

The front matter for the project files look like this (they have a date attribute):

---
title: First project
subtitle: This is the first project of the "projects" collection.
date: 2017-04-22
---

and

---
title: Second project
subtitle: This is the second project of the "projects" collection.
date: 2017-04-23
---

Currently the list generated is in chronological order i.e.

first project
second project

How could I reverse this list?

There will be many more projects, so I would like to use the date to organise them (and ideally not do it manually or with sort by as described here.

Thanks! k

have you tried using the sort filter:
{{ site.pages | sort: "title", "last" }} - obviously need to change it to your use case.

2 Likes

If you just want to iterate in the reverse order, you can use the reversed tag attribute:

{% for project in site.projects reversed %}
  <h3><a href="{{ project.url }}">{{ project.title }}</a><h3>
{% endfor %}
2 Likes

many thanks both!

I didn’t manage to work it out with ‘sort’ but, the ‘reversed’ option worked!

cheers! k