3

I am trying to display a list of all articles using liquid markup. I've got this code which displays them properly, however I want to be able to sort by the modified date descending (most recent article on top). How can this be accomplished?

I was thinking that perhaps I need to create a new array with all articles in it and then sort that, but I am not sure how to do that. Also note that I want to sort ALL of my articles by date, not just within each folder.

{% for category in portal.solution_categories %}
  {% if category.folders_count > 0 %}
    {% for folder in category.folders %}
      {% for article in folder.articles %}
           <a href="{{ article.url }}">{{ article.title }}</a> - {{ article.modified_on | short_day_with_time }} <br>
      {% endfor %}
    {% endfor %}
  {% endif %}
{% endfor %}

Thanks!

user3513237
  • 995
  • 3
  • 9
  • 26

1 Answers1

2

You can use a variable to sort the list of articles and then iterate that variable.

  {% for category in portal.solution_categories %}
      {% if category.folders_count > 0 %}
        {% for folder in category.folders %}
          {% assign sorted = (folder.articles | sort:date) %}
          {% for article in sorted %}
               <a href="{{ article.url }}">{{ article.title }}</a> - {{ article.modified_on | short_day_with_time }} <br>
          {% endfor %}
        {% endfor %}
      {% endif %}
    {% endfor %}
shaina
  • 212
  • 1
  • 10
  • That works, except that it is sorting the articles within each folder by date, but I want to sort all articles by date, ignoring the folder structure. Do you have a way to do that? – user3513237 Jul 06 '17 at 23:38