Hugo newbie here. For my site, I would like to display a display of articles from oldest to newest. The front matter of my index.md for each article contains a date field.
This the original template code (Stacks theme):
{{ define "main" }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ $pag := .Paginate ($filtered) }}
<section class="article-list">
{{ range $index, $element := $pag.Pages }}
{{ partial "article-list/default" . }}
{{ end }}
</section>
I changed the code above to sort .ByDate before pagination :
{{ define "main" }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
{{ $filtered := ($pages | intersect $notHidden) }}
{{ $sorted := $filtered.ByDate }}
{{ $pag := .Paginate ($sorted) }}
<section class="article-list">
{{ range $index, $element := $pag.Pages }}
{{ partial "article-list/default" . }}
{{ end }}
</section>
This works as I had intended (sorts from old to new) when I view my site using the Hugo web server. However, when I run the Hugo command to general the files for publication, the sorting doesn’t work (it sorts from new to old).
Can someone help me understand why the Hugo rendered file doesn’t sort in the same way as when I render the site using the Hugo server?
Other info:
hugo v0.111.3+extended linux/amd64 BuildDate=unknown
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.20.2"
github.com/sass/libsass="3.6.5"
github.com/webmproject/libwebp="v1.2.4"
git version 2.25.1