HUGO

Reverse range by value

I’m trying to generate a navigation menu like:

  • 2010 - 2020
  • 2000 - 2010
  • 1990 - 1999
  • 1980 - 1989
  • 1970 - 1979
  • 1960 - 1969
    • 1969
    • 1968
    • 1967
    • 1966
    • 1965
    • 1964
    • 1963
    • 1962
    • 1961
    • 1960
  • 1950 - 1959
    • 1959
    • 1958
    • 1957
    • 1956
    • 1955
    • 1954
    • 1953
    • 1952
    • 1951
    • 1950

So I started creating a sequence like:

  {{ seq 1950 10 $current_season }}

which creates a slice like: [1950 1960 1970 1980 1990 2000 2010 2020].

But I cannot seem to range it in reverse.

E.g {{ range sort (seq 1950 10 $current_season) "desc" }} won’t work.
sort seems to need a key to sort on, it does not allow to sort on value, right?

I cannot do something like {{ seq $current_season -10 1950 }} as in 2021 it will be [2021, 2011, ..].

I could do something like {{ seq (mul (math.Floor (div $current_season 10)) 10) -10 1950 }} but if I will ook at that code in 2 months time I will think what the heck :slight_smile:

Hi,

You should be able to do:

{{ range sort (seq ... ) "value" "desc" }}
2 Likes

Using "value" is some kind of pseudo reference to sort by value. Didn’t know that. Thx!

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