# Reverse Array

**URL:** <https://discourse.gohugo.io/t/reverse-array/28753>\
**Category:** support\
**Created:** [October 10, 2020, 1:35pm UTC](https://discourse.gohugo.io/t/reverse-array/28753 "2020-10-10T13:35:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [October 10, 2020, 1:35pm UTC](https://discourse.gohugo.io/t/reverse-array/28753/1 "2020-10-10T13:35:42Z")

</div>

How can I reverse a array in Hugo?

For example, lets say I have the following array:

```auto
{{ $myArray := slice "c" "b" "a" }}

```

I would expect I can do something like:

```auto
{{ $myArray := (slice "c" "b" "a") | reverse }}
<!-- Outputs: a b c -->

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 10, 2020, 11:25pm UTC](https://discourse.gohugo.io/t/reverse-array/28753/2 "2020-10-10T23:25:23Z")

</div>

layouts/partials/reverse-slice.html

```auto
{{- $sliceOriginal := . -}}
{{- $len := len $sliceOriginal -}}
{{- $sliceReversed := slice -}}
{{- range seq $len -}}
  {{- $sliceReversed = $sliceReversed | append (index $sliceOriginal (sub $len .)) }}
{{- end -}}
{{- return $sliceReversed -}}

```

Then call the partial from a template or shortcode:

```auto
{{- $myArray := slice "c" "b" "a" -}}
{{- $myArrayReversed := partial "reverse-slice.html" $myArray -}}

```

Reference:  
[https://discourse.gohugo.io/t/how-to-reverse-sort-frontmatter-list/23442/10](https://discourse.gohugo.io/t/how-to-reverse-sort-frontmatter-list/23442/10)

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [October 11, 2020, 10:25am UTC](https://discourse.gohugo.io/t/reverse-array/28753/3 "2020-10-11T10:25:27Z")

</div>

Awesome, this works great! Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [October 13, 2020, 10:25am UTC](https://discourse.gohugo.io/t/reverse-array/28753/4 "2020-10-13T10:25:31Z")

</div>

Closed on October 13, 2020, 10:25am UTC

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