---
title: Reversing the Order of a Loop in Hexo
date: 2019-05-17
tags: hexo, for loop
source: https://brianchildress.co/blog/reverse-order-in-hexo-loop
---

# Reversing the Order of a Loop in Hexo

## Problem

I need to reverse the order of posts that are displayed on my site based on their creation date, i.e. most recent first.

## Solution

With Hexo 3 we have the ability to reverse the order of items in a loop using _.each_ based on a specific parameter defined in the _front matter_ section of our site.

In this example we're using the date parameter of the post to sort all of the posts in reverse order. The syntax looks like this: `.sort('date', -1)`.

_Example_

```ejs
<% site.posts.sort('date', -1).each(function(item){ %>
     <%- item.title %></a>
<% }); %>
```
