Date sorting not working properly?

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.

{{ $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!

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

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

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

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 !

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:

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

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

{{ end }}
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.

Share your repository for further assistance