Sorting Param Array Range

Hi there, I’ve tried looking for existing queries on this topic but haven’t had much luck - hopefully someone can help!

In the frontmatter for my articles (running v0.61.0), I include academic references as an array - each reference contains an author, year, publisher, etc. and each article can have multiple references. The frontmatter looks like this:

  - Author: Smith, H.
    Title: The World's Religions
    Year: 1992
    Publisher: Harper Collins
    Location: London
  - Author: Durkheim, E.
    Title: The Elementary Forms of Religious Life (trans. K. Fields)
    Year: 1995 [1912]
    Publisher: The Free Press
    Location: New York

And in the page itself, I have the following code:

{{ range .Params.references }}
<li>{{ .Author }} ({{ .Year }}) {{ .Title }}, {{ .Location }}, {{ .Publisher }}.</li>
{{ end }}

This works fine, but doesn’t sort the references in any particular order - besides the order in which they appear in the front matter.

What I’d like to do is sort by author name, but when I try the following, I get an error message.

{{ range sort .Params.references "Author" "Asc"}}
<li>{{ .Author }} ({{ .Year }}) {{ .Title }}, {{ .Location }}, {{ .Publisher }}.</li>
{{ end }}

error calling sort: sequence must be provided

I can’t figure out why this isn’t working, but I’m sure I’m missing something obvious. Any ideas?

Do you have articles without references? It may be trying to sort a nil set.

Wrap your code in a with:

{{ with .Params.References }}
  {{ range sort . "Author" "asc" }}

You absolute star. That worked! Thanks so much :slight_smile: