Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to sort lists in Jekyll by two fields?

Is there a way to sort lists by two fields with Jekyll/Liquid? For example, sorting first by year and then title. I have tried:

{% assign list = site.data.papers.papers | sort: 'year' | sort: 'title' %}

but that left it sorted only based on the last field, in this case the title. There was some discussion on this, but it seems to have been frozen without being merged: https://github.com/jekyll/jekyll/issues/1802

Thanks!

like image 892
wonder Avatar asked Aug 12 '17 15:08

wonder


1 Answers

group_by to the rescue :

{% assign years = site.data.papers.papers | group_by: "year" %}
{% assign yearsSorted = years | sort: "name" %}
<ul>
{% for y in yearsSorted %}
  <li>{{ y.name }}
    <ul>
      {% assign yearTitlesSorted = y.items | sort: "title" %}
      {% for t in yearTitlesSorted %}
      <li>{{ t.title }}</li>
      {% endfor %}
    </ul>
  </li>
{% endfor %}
</ul>
like image 59
David Jacquel Avatar answered Oct 19 '22 02:10

David Jacquel