Creating and sorting slice of slices - is it possible?

I need to build array of uniform slices, each consists of

  • sort criteria
  • tag itself
  • display name
  • permalink

to sort it later by custom sort criteria asc or desc.

Just discovered that

{{ $tags := slice }}
{{ range $tag, $items := index site.Taxonomies $taxonomy }}
    {{ $tags = $tags | append (slice $sort $tag .Page.Params.fullname .Page.RelPermalink) }}
{{ end }}

creates $tags not two-dimensional array of slices, but one-dimensitonal array of strings addnig 4 new elements with each append.

Is it posible to create slice of slices? If not - what data structures are intended for my task in hugo data model? What’s about sorting of two-dimensional data structures in Hugo - does sort do the trick or I have to write sorting procedure (btw standard for most languages) myself?

How about a slice of maps?

{{ $tags := slice
  (dict "sortCriteria" "b" "name" "tag-b" "displayName" "Tag B" "permalink" "/tags/tag-b/" )
  (dict "sortCriteria" "c" "name" "tag-c" "displayName" "Tag C" "permalink" "/tags/tag-c/" )
  (dict "sortCriteria" "a" "name" "tag-a" "displayName" "Tag A" "permalink" "/tags/tag-a/" )

If you jsonify this it looks like:

    "displayName": "Tag B",
    "name": "tag-b",
    "permalink": "/tags/tag-b/",
    "sortCriteria": "b"
    "displayName": "Tag C",
    "name": "tag-c",
    "permalink": "/tags/tag-c/",
    "sortCriteria": "c"
    "displayName": "Tag A",
    "name": "tag-a",
    "permalink": "/tags/tag-a/",
    "sortCriteria": "a"

Then you can range/sort like this:

{{ range sort $tags "sortCriteria" "asc" }}
  {{ range $k, $v := . }}
    {{ printf "%s = %s" $k $v }}<br>
  {{ end }}<br>
{{ end }}

Which produces this:

displayName = Tag A
name = tag-a
permalink = /tags/tag-a/
sortCriteria = a

displayName = Tag B
name = tag-b
permalink = /tags/tag-b/
sortCriteria = b

displayName = Tag C
name = tag-c
permalink = /tags/tag-c/
sortCriteria = c

So, in my case append call should be performed with dict as parameter:

{{ $tags = $tags | append (dict "sort" $sort "tag" $tag "name" .Page.Params.fullname "link" .Page.RelPermalink) }}

This is the solution and It works perfect. Once again thank you for your efforts.

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