# Date sorting not working properly?

**URL:** <https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208>\
**Category:** support\
**Created:** [August 26, 2022, 2:06pm UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208 "2022-08-26T14:06:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![curtiswallen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/curtiswallen/32/15075_2.png) [@curtiswallen](https://discourse.gohugo.io/u/curtiswallen)\
**Post date:** [August 26, 2022, 2:06pm UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208/1 "2022-08-26T14:06:03Z")

</div>

Hi all,

I’m working on a website where there is a list of news items, and each news item can be associated with a specific person. Each person has their own page, and I want all associated news items to appear on the respective pages.

I’ve got the basic functionality working, as far as displaying the correct news items, but they are not being sorted in the proper chronological order. Hoping someone can spot the issue!

Also, very likely this code is very ugly and I’m going about this all wrong. Extremely open to suggestions in general to make things better overall.

```auto
{{ $last_name := .Params.last_name }}

{{ range sort (where $.Site.Pages "Section" "news") ".Params.article_date" "desc" }}

      {{ $link := .Params.article_url }}
      {{ $title := .Params.title }}
      {{ $date := .Params.article_date }}

      {{ range .Params.gallery_artists }}

        {{ if eq .fields.lastName $last_name }}

        <tr>
            <td><a href="{{ $link }}">{{ $title }}</a></td>
            <td>{{ dateFormat "01.02.2006" $date }}</td>
        </tr>

        {{ end }}

      {{ end }}

{{ end }}

```

The test news items I have are showing up as:

| Publication | Date |
| --- | --- |
| Artforum | 06.20.2022 |
| Artland | 10.01.2020 |
| Texte zur Kunst | 06.01.2022 |

Thoughts??

Thank you!

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [August 26, 2022, 3:13pm UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208/2 "2022-08-26T15:13:31Z")

</div>

Hugo supports `.Date`, `.ExpiryDate`, `.PublishDate`, and `.Lastmod` in a content’s front matter. Read more here

> **[Page Variables](https://gohugo.io/variables/page/)**
>
> Page-level variables are defined in a content file's front matter, derived from the content's file location, or extracted from the content body itself.

so, I guess this part should be like this, if you replace `article_date` with `date` in your front matter

```auto
{{ range sort (where site.Pages.ByDate.Reverse "Section" "news") }}

```

---

<div class="post-metadata">

**Author:** ![curtiswallen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/curtiswallen/32/15075_2.png) [@curtiswallen](https://discourse.gohugo.io/u/curtiswallen)\
**Post date:** [August 26, 2022, 3:37pm UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208/3 "2022-08-26T15:37:57Z")

</div>

Oh wow, yup that was it. I changed the “article\_date” to “date” and now it sorts perfectly with the following:

`{{ range where $.Site.Pages "Section" "news" }}`

Thank you, @tut !

---

<div class="post-metadata">

**Author:** ![curtiswallen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/curtiswallen/32/15075_2.png) [@curtiswallen](https://discourse.gohugo.io/u/curtiswallen)\
**Post date:** [August 26, 2022, 4:50pm UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208/4 "2022-08-26T16:50:32Z")

</div>

Changing all news items from “article\_date” to just “date” seemingly introduced another weird unexpected behavior.

My “/content/news” folder:  
artforum-0.517156698623364.md  
artland-0.6790129453553306.md  
gaylord-apartments-0.8303028017325611.md  
nir-altman-0.7600972178309426.md  
stevenson-gallery-0.22346809610368834.md  
texte-zur-kunst-0.9688844781834052.md  
whitney-biennial-2022-0.6855590598633556.md

Output when I try to get all news items:

```auto
{{ range where $.Site.Pages "Section" "news" }}

     <li>{{ . }}</li>

{{ end }}

```

```auto
Page(/news/artforum-0.517156698623364.md)
Page(/news/texte-zur-kunst-0.9688844781834052.md)
Page(/news/whitney-biennial-2022-0.6855590598633556.md)
Page(/news/artland-0.6790129453553306.md)

```

Any thoughts on why it would only return those four?? Before the front matter date name change it returned all of them. I can’t possibly think of why that would happen. The .md files are generated from a CMS using a script, so they are all structured identically.

Edit:  
I changed it back to “article\_date” in the front matter, and changed all corresponding code, and it’s loading all news items again. I am so confused.

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [August 27, 2022, 5:36am UTC](https://discourse.gohugo.io/t/date-sorting-not-working-properly/40208/5 "2022-08-27T05:36:55Z")

</div>

Share your repository for further assistance

> [@Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132#let-us-see-your-code-8):
>
> Please read and follow the below guidelines before asking for help in the forum. [DOCS](https://gohugo.io/documentation/) | [THEMES](https://themes.gohugo.io/) | [NEWS](https://gohugo.io/news/) | [GITHUB](https://github.com/gohugoio/hugo) | [FAQ](https://gohugo.io/troubleshooting/faq/) | [RELEASE NOTES](https://github.com/gohugoio/hugo/releases)How to Request Help Welcome to the Hugo forums and thanks for using Hugo! The below are a few friendly tips on getting help from the other Hugo volunteers in this forum. Some Knowledge Required Dare we say that while Hugo is indeed pretty magical, it is not meant to be a “magic wand” to suddenly give you a published website a la Square Space or Wordpress. You …
