Sorting article list by date on home page - cannot get it to work

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

Without seeing more of your repo or a reproducible test repository (i.e. you have not given enough context to do more than guess at this point) I would have to guess you are calling .Paginate somewhere else for the same page or are adding the pagination template to the page more than once.

Also original from where? Some theme?

Thanks for responding to my question. But why is the result different between the Hugo built in server vs exporting the files using Hugo? When the Hugo server is running, the site is displaying the order that I would like it to however when I export the files using the Hugo command, the generated index.html file has the reverse order.

I’m using the Stacks theme but the author doesn’t seem to be actively maintaining the theme anymore.

To answer that we need to see your repo as described in Requesting Help.

The answer does not (at least as far as I can tell) lie in the code you posted in code blocks in your question, but elsewhere in your repo (likely in combination with the Stacks theme). Therefore, we need to see the repo, or an example repo that consistently reproduces the error, to help.

Thanks, I’ll figure out how to share my repo (newbie, never done it before).

Decided that it’s easier / quicker for me to just manually save the index.html generated by the Hugo server and over-write the index.html generated by Hugo when exporting the site.