Sort 1,2,3 not 1,10,11

Is there a way to sort Params 1,2,3 instead of 1,10,11 ?

I am currently using the following code, and the chapters are numbered (in their front matter) 1, 2, 3, 4, 5 etc. However the code sorts them 1, 10, 11, 12, etc.

                <ul>
                    {{ range .Data.Pages.ByParam "chapter" }}
                    <li><b>Chapter {{.Params.chapter}}, Summary {{.Params.ordinal}}</b>
                            <br /> {{ .Content }}</li>
                    {{ end }}
                </ul>

Hello @kjrhody,

the code .Data.Pages.ByParam "chapter" gives a list with all pages that have a chapter defined in the front matter. How should Hugo know that chapter defines an order?

Hugo provides us a sort function to do exactly that. Try the following adaption:

{{ range .Data.Pages.ByParam "chapter" | sort .Params.chapter }}

Thank you @digitalcraftsman! I just tried that and it threw an error rendering the template at that line, saying: <sort .Params.chapter>: error calling sort: sequence must be provided

If it helps, chapter in the front matter looks like this:

+++
...
chapter="1"
...
+++

I didn’t see a way to set it to a string or integer, but not sure if that would help somehow?

The problem is that the sequence, i.e. the list of pages, has to be the first argument that’s passed to sort. Due to the piping the sequence became the last / second argument that’s passed.

I had a look at the documentation of sort and modified one of the examples:

{{ range sort (.Data.Pages.ByParam "chapter") ".Params.chapter" "asc" }}
1 Like

Thank you so much, that answers my original question!

I had also been using that second parameter Summary as a secondary sort, but now that is out of order

e.g:
Chapter 1, Summary 2
Chapter 1, Summary 4
Chapter 1, Summary 1
Chapter 1, Summary 3

Do you have any thoughts on how to sort that secondary parameter statement?

Using sort twice will not work. But Hugo has a GroupByParam function that allows us to group pages based on a given criteria, in your case it’s the chapter. In each group (chapter) we can sort the pages based on their summary. It’s like group by in SQL queries if you’re familiar with databases.

{{ range .Data.Pages.GroupByParam "chapter" }}
    {{ range sort .Pages "summary" }}
        {{ .Title }}
    {{ end }}
{{ end }}

@digitalcraftsman Thanks again for your quick reply! Unfortunately with that code I’m back to the original problem of it sorting the Chapters 1, 10, 11, etc. but it sorts the summaries correctly.

I will say it’s kind of weird that the summaries don’t sort automatically once the chapter has been sorted, because all of their weights are defined in the front matter and according to the documentation that’s the default initial method of sorting https://gohugo.io/templates/lists/#order-content

I set up a Hugo site based on your descriptions and got it working with the code below. My initial proposal would have worked if .Params.summary were used instead of summary. That’s the only change I’ve made.

{{ range .Data.Pages.GroupByParam "chapter" }}
  {{ range sort .Pages ".Params.summary" }}
      {{.Title }}
  {{ end }}
{{ end }}
1 Like

Thanks again for your help and your efforts. I also changed the “summary” argument. Not sure why, but when I use that code it sorts the summaries correctly but gives me the chapters in order 1, 10, 11, 12, etc. They have the correct weights set in their front matter (Chp. 1 = weight 1, Chp. 2 = weight 2, etc.) so even by default they should be sorting 1, 2, 3, etc.

So to reiterate:

{{ range .Data.Pages.GroupByParam "chapter" }}
   {{ range sort .Pages "Params.ordinal" }}
      Chapter {{ .Params.chapter }}, Summary {{ .Params.ordinal }} <br/>
   {{ end }}
{{ end }}

gives me

Chapter 1, Summary 1 
Chapter 1, Summary 2 
Chapter 1, Summary 3 
Chapter 1, Summary 4 
Chapter 1, Summary 5 
Chapter 1, Summary 6 
Chapter 10, Summary 1 
Chapter 10, Summary 2 
Chapter 10, Summary 3 
Chapter 10, Summary 4 
Chapter 11, Summary 1 
Chapter 11, Summary 2 
Chapter 11, Summary 3 
Chapter 11, Summary 4 
Chapter 11, Summary 5

I got it working without using weights in the front matter.

Isn’t the output in the last code block the output you expected?

Hm, that is so odd! No it isn’t what I expected, because I need it to sort the Chapters 1, 2, 3 and not 1, 10, 11. So if it was doing what I had hoped, it would look like this:

Chapter 1, Summary 1 
Chapter 1, Summary 2 
Chapter 1, Summary 3 
Chapter 1, Summary 4 
Chapter 1, Summary 5 
Chapter 1, Summary 6 
Chapter 2, Summary 1 
Chapter 2, Summary 2 
Chapter 2, Summary 3 
Chapter 2, Summary 4 
Chapter 3, Summary 1 
Chapter 3, Summary 2 
Chapter 3, Summary 3 
Chapter 3, Summary 4 
Chapter 3, Summary 5

Both outputs are sorted the same way. What surprises me is that the first output (wth chapters 1, 10, 11) doesn’t include chapters 2 and 3. The same goes for the output with chapter 1,2,3 the other way around.

Is it possible to share current Hugo site (or a stripped down version), e.g. on a (temporary) repository on GitHub?

Well it does include chapters 2, 3, etc but they are after the numbers that start with 1. There are 15 chapters and it sorts them like this:

1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8 , 9

And I need them sorted like:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

Unfortunately I can’t really share the site code at the moment but I might be able to set up a stripped down version soon. Info that might help in the meantime, but which I think I’ve already shared throughout the thread:

  • It’s in a list.html template for a specific content type (’summary’)
  • There are several summaries in each chapter
  • Both the chapter # and the summary # are in the front matter for the summary content type
  • Each chapter (’chapter’ is another content type) has a number and a weight (in addition to other info) in its own front matter

I’ve set up a simple repository that might clarify a few things. One the homepage, click on the link that takes you to the list template of the “summary” content type.

https://github.com/digitalcraftsman/hugo-forum-example

Thank you again for your efforts to help solve this! I checked it out and it looks like the themes/hyde directory is empty (expecting a layouts/summary/list.html template)? My content files are set up the same (with more data in the front matter, but that’s it).

I forgot to track the theme as well. The repo has been updated and you should be able to see the theme as well.

@digitalcraftsman’s example works for me. And to make sure it works correct, one needs to add chapter 10, to ensure it falls into the correct place. It does:

chapter-summary

Yeah I’m not really sure what’s going on with my site, that same exact code sorts it incorrectly. I guess I’ll have to do a more thorough review of my site to see what I’m missing here.

It looks like I’ve found the answer. The reason that @digitalcraftsman’s code works is because his content files are set up like this:

---
chapter: 1
summary: 1
--- 

Mine are set up like this:

+++
chapter = "1"
summary = "1"
+++

When I changed his front matter setup to be the same as mine, it began sorting the chapters 1, 10, etc. instead of 1, 2, etc. Apparently it all has to do with whether the numbers are in quotes or not. I haven’t seen any documentation on this for Hugo, and the sorting works for these items everywhere else on the site.

As I mentioned in reply #3 to this thread, I wasn’t sure if there was some issue between setting a chapter # as a string or integer, but didn’t see how to do it?

1 Like

Hugo’s seems to handle strings ("1") and integers (1) differently. The devil is in the detail :wink:

1 Like