# Sorting and grouping json objects

**URL:** <https://discourse.gohugo.io/t/sorting-and-grouping-json-objects/5494>\
**Category:** support\
**Created:** [February 17, 2017, 3:31pm UTC](https://discourse.gohugo.io/t/sorting-and-grouping-json-objects/5494 "2017-02-17T15:31:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flashpixx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/flashpixx/32/1795_2.png) [@flashpixx](https://discourse.gohugo.io/u/flashpixx)\
**Post date:** [February 17, 2017, 3:31pm UTC](https://discourse.gohugo.io/t/sorting-and-grouping-json-objects/5494/1 "2017-02-17T15:31:14Z")

</div>

Hello,

I have got a JSON data structure with

```
{
    "a long key 1" : {
                                  "group" : "keyprefix1"
                                  "description" : "a text"
   },
  "another key 2" : {
                                  "group" : "keyprefix2"
                                  "description" : "a text"
  },
  ...
}

```

I I would like to create a HTML list:

```
<ul>
     <li> keyprefix 1
           <ul>
                // here all elements with keyprefix1
           </ul> 
    </li>
     <li> keyprefix 2
           <ul>
                // here all elements with keyprefix2
           </ul> 
    </li>

```

How can I group all JSON objects with equal attribute “group”?

Thanks

---

<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:** [February 17, 2017, 9:58pm UTC](https://discourse.gohugo.io/t/sorting-and-grouping-json-objects/5494/2 "2017-02-17T21:58:47Z")

</div>

Have you tried using the `sort` function? If `sort` doesn’t support your use case, we can look at extending it.

```auto
{{ range sort .Data.whatever "group" }}

```

---

<div class="post-metadata">

**Author:** ![flashpixx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/flashpixx/32/1795_2.png) [@flashpixx](https://discourse.gohugo.io/u/flashpixx)\
**Post date:** [February 17, 2017, 10:07pm UTC](https://discourse.gohugo.io/t/sorting-and-grouping-json-objects/5494/3 "2017-02-17T22:07:43Z")

</div>

I have used sort, but sort removes the key value of the json document. The sort function replaces the key with a number:

```
 range $key, $value := .Data.document

```

In this case I get in $key, the json key values but in

```
range $key, $value := sort .Data.document "group"

```

I get in $key a number in [0, number of objects), so I lost the key. I have put inside the $value the key also and so I can solve the problem
