How to sort and order a Jekyll collection

Snippets to index a Jekyll collection

For the sake of this example, let’s assume we have a collection called projects.

Sort a Jekyll collection in ascending order

Here is how we can get a collection posts in ascending order on a page or index (oldest collection post first).

{% assign projects = site.projects %}

{% for project in projects %}

Sort a collection in reverse order

Sorting a Jekyll collection in reverse order (latest collection post first) is pretty straightforward in Jekyll.

{% assign projects = site.projects %}

{% for project in projects reversed %}

Sort a collection in reverse order and limit the number of results

Now let’s look at how we can limit the number of posts from a collection and sort the result in descending order (latest collection post first). This is useful if you have a site with hundred of articles and want to create an index with the latest results in a given collection.

{% assign sorted = site.projects | sort: 'date' | reverse  %}

{% for project in sorted limit: 12 %}

If you leave out | reverse, your collection will show up oldest post first.

Note that these snippets don’t give you a pagination system.