How to sort in reverse order by preserving the order?

Now I understand.

data/test.yaml

- url: "B.jpg"
  caption: "This is B"
- url: "A.jpg"
  caption: "This is A"
- url: "C.jpg"
  caption: "This is C"

template

{{- $data := .Site.Data.test -}}
{{- $keysInReverseOrder := seq (sub (len .Site.Data.test) 1) -1 0 -}}
{{- range $keysInReverseOrder -}}
  key = {{ . }}<br>
  caption = {{ index $data . "caption" }}<br>
  url = {{ index $data . "url" }}<br><br>
{{- end -}}

result

key = 2
caption = This is C
url = C.jpg

key = 1
caption = This is A
url = A.jpg

key = 0
caption = This is B
url = B.jpg

Credit to @oncletom for posting this solution:

1 Like