How to sort in reverse order by preserving the order?

I’ve a list of image links and captians in data files like:

- url: "2_zc9wes.jpg"
  caption: ""
- url: "1_zkrzpf.jpg"
  caption: ""
...

There are 57 of total and what I want is to display them in reverse order like 57-1 instead of 1-57. I tried sort function with desc argument-which is what I need- but nothing happening and not even any error and I tried here but couldn’t figure out what to do with the map[captian: url: "1_zkrzpf.jpg"

data/test.yaml

- url: "2_zc9wes.jpg"
  caption: ""
- url: "1_zkrzpf.jpg"
  caption: ""

template

{{- range $k, $v :=  sort .Site.Data.test "url" "desc" -}}
  k = {{ $k }}<br>
  caption = {{ index $v "caption" }}<br>
  url = {{ index $v "url" }}<br><br>
{{- end -}}

result

k = 0
caption =
url = 2_zc9wes.jpg

k = 1
caption =
url = 1_zkrzpf.jpg
2 Likes

In addition to @jmooring 's answer, one can also call the parameter values like so:

{{- range $k, $v :=  sort .Site.Data.test "url" "desc" -}}
  k = {{ $k }}<br>
  caption = {{ .caption }}<br>
  url = {{ .url }}<br><br>
{{- end -}}
2 Likes

Sorry it’s not perfect.
See pic 1 see before


and after

I’m using the simple range in before

{{ range sort .Site.Data.zweites }}
  {{ partial "figure.html" . }}
{{ end }}

Please see my answer above

I do not understand.

If you are still having a problem, please clarify your problem statement. Screen captures of images are not helpful.

If your problem is resolved, please mark the best answer in the thread using the checkbox icon from the comment action menu.

I also don’t understand your answer. Screenshots are not enough and the quoted code is not clear about what you are doing since it shows a call to the partial figure.html.

You need to share the project and be descriptive as mentioned in the Requesting Help guidelines.

Yes, problem isn’t solved yet. I thought the screenshot will help better. I asked for how to get images in reverse order? Like the first image in the .Site.Data.zweites should be displayed as last image in the gallery. The method you guys told me is not doing it properly like if you check the screenshots you’ll see the first image in screenshot 1 is the 3rd in row 2 from the bottom but it’d be the last one and the last one should be coming at position 1.
Suppose there are totally the 4 images in order:

1.jpg
2.jpg
3.jpg
4.jpg

and I simply want them to be ordered like (on the basis of their position index not name)

4.jpg
3.jpg
2.jpg
1.jpg

For example, in the similar fashion as the file manager do like in descendant order.


I hope, now you’ll understand me better.
Thank you for the help!

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

Thanks for the help!

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