Filter and Sort in Jekyll

This post will describe how to filter and sort a set of Jekyll pages. I needed to do this recently because I wanted to display a set of Jekyll pages stored in folders that didn’t include every page and was sorted in a defined order. Initially I came across an excellent article that described the basic syntax but I encountered some Liquid errors perhaps because I’m using Jekyll 3. The solution I came up with was used to create the projects page:

YAML Front Matter

I decided to filter pages by layout and sort by a new front matter variable called “order”, so each of my pages looked like this:

---
layout: projects
order: 2
---

Jekyll Code

Next, I used the following Jekyll code to sort and filter:

{% assign projects = site.pages | where: "layout", "projects" | sort: "order" %}
{% for page in projects %}
    // do stuff
{% endfor %}

So, based on the order specified by the Jekyll YAML front matter, the loop iterates in increasing order.