Same date sorts differently based on front matter in markdown

I have this project structure after running hugo new and adding minimal working files:

project
│   config.yaml    
│
└───layouts
│   │   index.html
│   │
│   └───_default
│       │   baseof.html
│   
│   └───layouts
│       │   head.html
│       │   recent.html
│       │   script-footer.html
│   
└───content
│   └───posts
│       │   └───2022-10-31
│       │       │   index.md
│       │   └───2023-05-28
│       │       │   index.md
│       │   └───2023-05-28 copy
│       │       │   index.md
│   

md:title=layouts/partials/recent.html

<div>
    <hr class="solid">
    {{ $bydate := (where .Site.Pages "Section" "posts") }}
    {{ range sort $bydate  ".Params.start" }}
        {{ $dateTime := .Params.start  }}
        {{ $title := .Params.title}}
        {{ if $dateTime }}
            <div> 
                <p style="font-size:12px">
                    <time datetime="{{ $dateTime }}">{{ $dateTime | time.Format ":date_long" }}</time>
                    <t/>
                    {{ $title }}
                </p>
            </div>
        {{end}}
    {{end}}
    <hr class="solid">
</div>

md:title=content/posts/2022-10-31/index.md

---
draft: false
title: "Summer"
start: "2022-10-31T00:00:00+11:00"
---

md:title=content/posts/2023-05-28/index.md

---
draft: false
title: "Depth"
start: "2023-05-28T00:00:00+11:00"
---

md:title=content/posts/2023-05-28 copy/index.md

---
draft: false
title: "Summer"
start: "2023-05-28T00:00:00+11:00"
---

This results in the following screenshot:


I expected both list items “2023-05-28T00:00:00+11:00” to sort at the same level (they don’t?!). If the Dates are being treated as Strings they should still sort together. I tried these combinations that do not work:

---   {{ range sort $bydate  ".Params.start" }}
+++  {{ range sort $bydate  (time ".Params.start") }}
+++  {{ range sort $bydate  (".Params.start" | time) }}

Code Repository

First, by using .Pages instead of .RegularPages you are including the section page, where the param value is <nil>.

Second, you aren’t coding defensively around pages without the start command.

Third, the dates in YAML, whether quoted or not, are strings. Sorting dates as strings is OK if your format is consistent, which it is in your example. With TOML front matter, unquoted dates are first class citizens.

Try this (it only addresses my first point):

<div>
  <hr class="solid">
  {{ range sort (where site.RegularPages "Section" "posts") "Params.start" }}
    {{ $dateTime := .Params.start | time.AsTime }}
    {{ $title := .Params.title }}
    {{ if $dateTime }}
      <div>
        <p style="font-size:12px">
          <time datetime="{{ $dateTime }}">{{ $dateTime | time.Format ":date_long" }}</time>
          <t/>
          {{ $title }}
      </p>
      </div>
    {{ end }}
  {{ end }}
  <hr class="solid">
</div>

To see what’s happening:

{{ range $k, $v := sort (where site.Pages "Section" "posts") "Params.start" }}
  {{ $k }} = {{ $v }} start = {{ printf "%[1]v (%[1]T)" .Params.start }}<br>
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.