How to sort yaml data by specific param

I’m ranging over a yaml file like this:

    {{ range (index .Site.Data .Params.gallery) }}
      {{ range . }}

Here’s a sample of some entries from the yaml:

- src: "210723.mp3"
  title: "7/23/2021"
  date: 210723
- src: "210724.mp3"
  title: "7/24/2021"
  date: 210724
- src: "210727.mp3"
  title: "7/27/2021"
  date: 210727

I want to display the results in order using the “date” attribute since that will place them in the correct chronological order. At the moment it displays them alphabetically (i.e. 2/2022 comes before 7/2021)

See https://gohugo.io/functions/sort/#readout. For example:

{{ range sort site.Data.foo "date" }}
  ...
{{ end }}
1 Like

using that method to range on foo.yaml I get the following error:

<.Site.Data.foo>: foo is not a method but has arguments

Please post a link to the public repository for your project.

I don’t have one at the moment.

That is going to make troubleshooting somewhat difficult.

1 Like