Sort a hash map with arbitrary keys

Hi everyone,

This is my first post here so I hope I won’t do it for a bad reason but as I did not find the solution in the documentation I figured someone could help me here :).

I’m using Hugo for a french website hosting a podcast, source is available on github: https://github.com/comptoirsecu/csec-hugo

I’m using an open source player called Podigee for our audio files, which support defining chaptermarks. And the best solution I’ve found to define those chaptermarks is to define them in my frontmatter as a hash map on the following format:

chaptermarks:
“hh:mm:ss”: “My chaptermark title”

You can find an example on this markdown file: (I’m removing the http as I’m not allowed to have more than 2 links) comptoirsecu/csec-hugo/blob/master/content/sechebdo/2017-07-25-sechebdo-25-juillet-2017.md

This works fine for the web player, but I’m also using it in a shortcode in order to print out the podcast’s summary in my article: comptoirsecu/csec-hugo/blob/master/layouts/shortcodes/chaptermarks.html

The important bit being:

 {{ range $start, $chapter :=  . }}
       <li>{{ $chapter }} <strong><a href="#t={{$start | safeURL}}" onclick="location.reload()">({{$start}})</a></strong></li>
  {{ end }}

The only issue is that it is printing the summary items in random order (I was pretty sure it was ordered by the key earlier, e.g. the timecode in my example, but now it is not the case anymore).

According to the documentation, it is possible to go through a map while sorting its keys or values in ascending or descending order : https://gohugo.io/functions/sort/

However, all the example are using range without specifying variables for the key and value as I’m doing with $start and $chapter. I could not find a way to add sorting in my range without breaking it.

Is it impossible to do? Am I missing something obvious?

Thanks in advance for your help!

Just as an update, I tried to simply sort my hashmap in advance to then range over it, the issue is that sorting a map results in a slice that contains sorted values, but the keys are stripped away, as it is written in the doc.

Is there any trick I could use to save them? Is there a way to maybe concatenate the key and value with a special char in the middle, like ‘|’, then sort the whole thing, then range over it and split it again?