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

**URL:** <https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953>\
**Category:** support\
**Tags:** pagination\
**Created:** [April 18, 2023, 1:36am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953 "2023-04-18T01:36:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chanster](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chanster/32/19270_2.png) [@chanster](https://discourse.gohugo.io/u/chanster)\
**Post date:** [April 18, 2023, 1:36am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/1 "2023-04-18T01:36:31Z")

</div>

**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](https://github.com/CaiJimmy/hugo-theme-stack)):

```auto
{{ 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 :

```auto
{{ 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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [April 18, 2023, 2:24am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/2 "2023-04-18T02:24:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [April 18, 2023, 2:28am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/3 "2023-04-18T02:28:11Z")

</div>

> [@chanster](#):
>
> This the original template code

Also original from where? Some theme?

---

<div class="post-metadata">

**Author:** ![chanster](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chanster/32/19270_2.png) [@chanster](https://discourse.gohugo.io/u/chanster)\
**Post date:** [April 18, 2023, 4:29am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/4 "2023-04-18T04:29:24Z")

</div>

> [@cshoredaniel](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![chanster](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chanster/32/19270_2.png) [@chanster](https://discourse.gohugo.io/u/chanster)\
**Post date:** [April 18, 2023, 4:30am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/5 "2023-04-18T04:30:17Z")

</div>

> [@cshoredaniel](#):
>
> > [@chanster](#):
> >
> > This the original template code
> 
> Also original from where? Some theme?

I’m using the [Stacks theme](https://github.com/CaiJimmy/hugo-theme-stack) but the author doesn’t seem to be actively maintaining the theme anymore.

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [April 18, 2023, 7:37am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/6 "2023-04-18T07:37:51Z")

</div>

> [@chanster](#):
>
> But why is the result different between the Hugo built in server vs exporting the files using Hugo?

To answer that we need to see your repo as described in [Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132).

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.

---

<div class="post-metadata">

**Author:** ![chanster](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chanster/32/19270_2.png) [@chanster](https://discourse.gohugo.io/u/chanster)\
**Post date:** [April 18, 2023, 7:46am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/7 "2023-04-18T07:46:47Z")

</div>

> [@cshoredaniel](#):
>
> > [@chanster](#):
> >
> > But why is the result different between the Hugo built in server vs exporting the files using Hugo?
> 
> To answer that we need to see your repo as described in [Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132).
> 
> 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).

---

<div class="post-metadata">

**Author:** ![chanster](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chanster/32/19270_2.png) [@chanster](https://discourse.gohugo.io/u/chanster)\
**Post date:** [April 18, 2023, 8:25am UTC](https://discourse.gohugo.io/t/sorting-article-list-by-date-on-home-page-cannot-get-it-to-work/43953/8 "2023-04-18T08:25:51Z")

</div>

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.
