Skip to content

Instantly share code, notes, and snippets.

@mariordev
Last active August 17, 2020 23:31
Show Gist options
  • Save mariordev/5fc1391211c9a4df3b5826abf72aa8b1 to your computer and use it in GitHub Desktop.
Save mariordev/5fc1391211c9a4df3b5826abf72aa8b1 to your computer and use it in GitHub Desktop.
Quick example of how to sort data from a json file in Jekyll
<!-- In this example site.data.items points to a file named items.json in the _data folder -->
<!-- Replace "date" with the name of the field you want to sort by -->
{% assign sortedItems = site.data.items | sort: "date" | reverse %}
<ul>
{% for item in sortedItems %}
<li>{{ item.title }} {{ item.date }}</li>
{% endfor %}
</ul>
@ggrantjr
Copy link

Thank you for the response!

I tried this approach on my page, but the data is not being retrieved.

I entered the following:

    {% assign sortedItems = site.data.events.project1 | sort: "date" | reverse %}
    <section>
        {% for item in sortedItems %}
        <p>{{ item.date }}</p>
        <p>{{ item.title }}</p>
        {% endfor %}
    </section>

My data file is located as follows:

/_data/events/project1.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment