Sort is not working in a consistent manner

Hello, I have some version values in my config.tml. These are either x.y or saas. There can be any number of x.y values. What I want is to obtain the latest x.y and use this in my saas pages. Here is an example.

[params.menus.saas]
path = "saas"
label = "SaaS version"

[params.menus.1_0]
path = "1.0"
label = "Self-hosted 1.0"
semver = "1.0.4"

[params.menus.1_1]
path = "1.1"
label = "Self-hosted 1.1"
semver = "1.1.3"

Here is my template in a shortcode.

{{- range $key, $value := sort .Site.Params.menus "semver" "desc" -}}
{{- if eq $key 1 -}}
{{ printf "release-%s" .semver }}
{{- end -}}
{{- end -}}

Site.Params.menus is an array of maps. I want to sort it by the semver value. This value may or may not be present. Unfortunately, the sort does not work in a predictable manner. I include this shortcode in three places in a single page. I am getting a different sort at times, so that I cannot consistently select the correct value. Is this perhaps a known issue? We are on Hugo 59.1.

At first glance it seems that you are calling a non-existent parameter with the range and sort functions since you have .Site.Params.menus.1_0 and .Site.Params.menus.1_1 but apparently no .Site.Params.menus is present in the provided code block.

However this is not the way of asking for help in this forum.
Especially when making bold statements about sort not working consistently.

You need to provide a repo for users to see the project in question.

OK, I realize this claim may be bold, however, it is reproducible. Our repo is private, however, I have a partial copy of it here https://github.com/emanic/material-docs I have reproduced this behavior in the dummy repo. You may need to rebuild the site and refresh your browser to observe the behavior of the sort: it is highly variable.

Your problem has nothing to do with sort.
The following is not quite right:

{{- if eq $key 1 -}}
{{ printf "release-%s" .semver }}
{{- end -}}

In the above you are checking if the semver key exists and then you overwrite it.

To print all semver versions -while checking for the existence of the key- replace the above with the following:

    {{ with .semver }}
      {{ printf "release-%s" . }}
    {{ end }}

This topic was automatically closed after 16 hours. New replies are no longer allowed.