Error on sorting a a slice from Scratch

Im trying to create a section overview sorted by semvar versions. The version is part of the front matter and i would like so to sort the overview in descending order.

I created a template but unfortunately the build step throws an error: error calling sort: sequence must be provided

This is the code i am using:

{{ range (.Paginator.Pages.ByParam "version").Reverse }}
    {{/* split version string */}}
    {{ $v_major       :=  int (replaceRE "([0-9]+)\\.[0-9]+\\.[0-9]+" "${1}" .Params.version) -}}
    {{ $v_minor       :=  int (replaceRE "[0-9]+\\.([0-9]+)\\.[0-9]+" "${1}" .Params.version) -}}
    {{ $v_minor_minor :=  int (replaceRE "[0-9]+\\.[0-9]+\\.([0-9]+)" "${1}" .Params.version) -}}

    {{/* add leading zeros */}}
    {{ $version := printf "%08d%08d%08d" $v_major $v_minor $v_minor_minor }}

    {{/* put all versions into an array */}}
    {{ $.Scratch.Add "versions" (slice ($version)) }}


{{ range sort ($.Scratch.Get "versions") "value" "desc"}}
    {{ printf "%+v" . }}
{{ end }}

Am i doing something wrong here or is this use case not intended for the built in sort function?
Is there another solution for reverse ordering a custom slice?

The Scratch function GetSortedMapValues would be exactly what is am needing, if it had a reverse option.

I created three test pages, placing a version field in the front matter (TOML) of each page, something like this:

version = "n.n.n"

The actual values I used were:


Then I pasted your code into list.html. Result:


I’m not sure where the problem is. I suggest you post a link to a public repository for your project. Unless I’m doing it wrong…

Thanks for your help!

Turned out the error was somewhere else. The list template was also used by other pages that did not contain version fields. So for this pages the slice was always empty and the error was thrown. Wrapping the code with an if statement solved the issue.

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