Sorting pages by last modified date in Hugo

This blog is managed as a git repository and I use Hugo as the framework for managing it. I am using the Hugo classic theme which I have tweaked slightly and store it along with the blog source.

I wanted to modify the sorting of the blog post titles on the index page so that the most recently modified page was displayed first. The support was added to Hugo 4 years back, so the following template worked for me:

  {{ range (where .Data.Pages.ByLastmod.Reverse "Section" "!=" "") }}
    <span class="date">{{ .Date.Format "2006/01/02" }}</span>
    <a href="{{ .URL }}">{{ .Title }}</a>
  {{ end }}

The key bit here is: .Data.Pages.ByLastmod.Reverse. My blog’s config file has the following key information which is needed for the above to work:

enableGitInfo = true
lastmod = ["lastmod", ":fileModTime", ":default"]