# Sorting returned data

**URL:** <https://discourse.gohugo.io/t/sorting-returned-data/14730>\
**Category:** support\
**Created:** [October 16, 2018, 3:51pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730 "2018-10-16T15:51:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [October 16, 2018, 3:51pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/1 "2018-10-16T15:51:12Z")

</div>

Having trouble figuring out how to sort my returned JSON data. I’m trying to sort the data by the `$element.modifier` values. But everything I’ve tried in inserting `sort` has returned errors. Where should I be doing this?

(I tried putting `{{ range sort . $element.modifier }}` after the initial `range` but that didn’t work either.)

JSON data

```auto
{
    "props": {
        "NAME": {
            "value": "16px",
            "desc": "",
            "css": "padding",
            "modifier": "primary",
            "function": "",
            "color": "",
            "group": "",
            "element": "content",
            "breakpoint": "",
            "state": "",
            "environment": "",
            "hex": ""
        },
        ...
    }
}

```

Shortcode template:

```auto
{{ $pattern := .Get "pattern" }}
{{ $data := index .Site.Data.design_tokens $pattern }}
{{ range $index, $element := $data.props }}
Lines with {{ $element.modifier }} and more...
{{{ end }}

```

---

<div class="post-metadata">

**Author:** ![dirkolbrich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dirkolbrich/32/6968_2.png) [@dirkolbrich](https://discourse.gohugo.io/u/dirkolbrich)\
**Post date:** [October 16, 2018, 8:30pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/2 "2018-10-16T20:30:54Z")

</div>

Does this thread helps?

> [@Ranging over collection of data files](https://discourse.gohugo.io/t/solved-ranging-over-collection-of-data-files/14488/9):
>
> A good nights sleep helped to think about this. range accepts maps, arrays and slices, first and where only accept an array or a slice. The data in the data/ folder [will be accessible as a map in the .Site.Data variable](https://gohugo.io/templates/data-templates/). A map by Golang definitions is an unordered container for data, the order within that container is quite random. Posts in the content/ folder on the other side will be parsed into a slice of type Pages []\*Page with a default order [Default: Weight \> Date \> LinkTitle \> Fil…](https://gohugo.io/templates/lists/#default-weight-date-linktitle-filepath)

---

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [October 16, 2018, 9:18pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/3 "2018-10-16T21:18:01Z")

</div>

Thank you for replying, but it doesn’t in the sense that I’m trying to sort the (kind of) already sorted returned data. I really just want to display the data according to one field alphabetically, or ideally, multiple fields concurrently.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [October 16, 2018, 10:43pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/4 "2018-10-16T22:43:49Z")

</div>

Thinking outside the hugo box (and noting that you’re trying to it with hugo directly), you might look into `jq`, which is a utility for manipulating json data. I have found use for it in many projects. [https://stedolan.github.io/jq/](https://stedolan.github.io/jq/)

---

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [October 17, 2018, 5:47pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/5 "2018-10-17T17:47:38Z")

</div>

Thanks @RickCogley. I’ll take a look at it.

Still would be very surprised if this wasn’t fairly easy with Hugo, however.

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [October 17, 2018, 6:26pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/6 "2018-10-17T18:26:41Z")

</div>

Try:

```auto
{{ range $index, $element := sort $data.props "modifier" }}

```

---

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [October 17, 2018, 7:07pm UTC](https://discourse.gohugo.io/t/sorting-returned-data/14730/7 "2018-10-17T19:07:51Z")

</div>

> [@moorereason](#):
>
> {{ range $index, $element := sort $data.props “modifier” }}

@moorereason Yes! Thank you! This is the simple solution I’ve been looking for, and can’t believe I didn’t try. (or if I did, I likely missed some syntax bug).
