Preserve the order of frontmatter maps

Hello,

I have a list of older releases in my frontmatter as such

"olderReleases": {
    "2020": "2019-06-11",
    "2018": "2018-08-16",
    "2017": "2017-10-10",
    "2016-1": "2016-12-17",
    "2016": "2016-02-22",
    "2015": "2015-05-27",
    "2014": "2014-08-28",
    "2013-2": "2013-12-11",
    "2013-1": "2013-11-17",
    "2013": "2013-02-16",
    "2012": "2012-05-24",
    "2011-1": "2011-10-11",
    "2011": "2011-02-11",
    "2009-2": "2010-06-30",
    "2009-1": "2009-12-12",
    "2009": "2009-04-29",
    "2008": "2008-06-10",
    "2007": "2007-11-27",
    "2005": "2005-10-14",
    "2004": "2004-05-21",
    "2003": "2004-03-19"
}

I’d like to display it in the exact same order but hugo/go seems to parse and order the dictionary as:

{{ with .Params.olderReleases }}
      {{ . }}
{{ end }}

outputs

map[2003:2004-03-19 2004:2004-05-21 2005:2005-10-14 2007:2007-11-27 2008:2008-06-10 2009:2009-04-29 2009-1:2009-12-12 2009-2:2010-06-30 2011:2011-02-11 2011-1:2011-10-11 2012:2012-05-24 2013:2013-02-16 2013-1:2013-11-17 2013-2:2013-12-11 2014:2014-08-28 2015:2015-05-27 2016:2016-02-22 2016-1:2016-12-17 2017:2017-10-10 2018:2018-08-16 2020:2019-06-11]

I’ve tried sort and .Reverse but neither work. I’ve thought about manually rangeing over a seq (len .) 0 but I don’t think I can index the rows from the map.

I think a solution would be to store the map as two associated arrays, however I would prefer to keep the data structure as is for maintainability.

Many thanks, Carlin

Hi there,

What you have there is a map, which is by definition an unordered collection.

That’s sad to hear, coming from python which just got ordered dictionaries. Thanks though! I’ll use associative arrays instead

If you have control over input, you could do something like:

"olderReleases": [
    {"2020": "2019-06-11"},
     ...
]

which would preserve order.

1 Like

oh yep I can definitely do that instead, and keeps the semantics of the data. Thank you!

{{ with .Params.olderReleases }}
  {{ range $map := . }}
    {{ range $key, $value := $map }}
      {{ $key }}: {{ $value }}
  {{ end }}
{{ end }}

for completion, this is how to access the new format

1 Like

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