Reverse the order of an Eleventy Collection

Ed's Blog Index & Contents

Aug 24, 2021 7:00

Reverse the order of an Eleventy Collection

See my blog post "How to create an Eleventy Collection" where I show you how I created the golf collection. I wanted it sorted from Highest to lowest, in this case from the current year down to 2003.

I found The "sort" filter which is a nunjuck filter with the following syntax:

sort(arr, reverse, caseSens, attr)

arr is an array with JavaScript's arr.sort function. If the 2nd attribute, reverse is true the result will be reversed. Sort is case-insensitive by default, but setting caseSens to true makes it case-sensitive. If attr is passed, will compare attr from each item.

Nunjuck Sort

I set Reverse to true and I passed in a 3rd argument 'date' so it would sort by the date of the collection

Reverse using the sort filter

this works. It reversed the collection and printed 2021 first