# Sort two parameters inside data

**URL:** <https://discourse.gohugo.io/t/sort-two-parameters-inside-data/26636>\
**Category:** support\
**Created:** [July 1, 2020, 12:56pm UTC](https://discourse.gohugo.io/t/sort-two-parameters-inside-data/26636 "2020-07-01T12:56:59Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [July 2, 2020, 3:31pm UTC](https://discourse.gohugo.io/t/sort-two-parameters-inside-data/26636/5 "2020-07-02T15:31:07Z")

</div>

This post from @FelicianoTech was really helpful:

> [@GroupBy For Data Files in Hugo](https://discourse.gohugo.io/t/groupby-for-data-files-in-hugo/21893):
>
> Cross-posted from [my blog](https://www.feliciano.tech/blog/groupby-for-data-files-in-hugo/) I recently had the need to list out a bunch of locations on a page with Hugo. With over 100 locations to display, I wanted to group them by state on the page creating a kind of directory. The first thing that came to mind was Hugo’s [GroupBy](https://gohugo.io/templates/lists/#group-content) function. Unfortunately I’ve learned that the GroupBy function only works on Hugo Pages while I’m storing all of these locations in a Hugo Data File. Here’s how I solved this problem. Now, my solution is no where near as neat…

data/portfolio\_residential.json:

```txt
[
  {
    "favorite": "3",
    "project_name": "D"
  },
  {
    "favorite": "3",
    "project_name": "C"
  },
  {
    "favorite": "1",
    "project_name": "B"
  },
  {
    "favorite": "1",
    "project_name": "A"
  },

  {
    "favorite": "2",
    "project_name": "E"
  },
  {
    "favorite": "6",
    "project_name": "F"
  },
  {
    "favorite": "5",
    "project_name": "G"
  },
  {
    "favorite": "4",
    "project_name": "I"
  },
  {
    "favorite": "4",
    "project_name": "H"
  }
]

```

Template:

```go-html-template
{{/* Build slice of unique, sorted favorites. */}}
{{- $favorites := slice -}}
{{- range .Site.Data.portfolio_residential -}}
  {{- $favorites = $favorites | append .favorite -}}
{{- end -}}
{{- $favorites = $favorites | uniq | sort -}}

{{/* Range over the sorted favorites, then range over a sorted subset of the data file. */}}
{{- range $favorites -}}
  {{- range where (sort $.Site.Data.portfolio_residential "project_name" "asc") "favorite" . -}}
    favorite = {{ .favorite }}<br>
    project_name = {{ .project_name }}<br><br>
  {{- end -}}
{{- end -}}

```

---

_[View the full topic](https://discourse.gohugo.io/t/sort-two-parameters-inside-data/26636)._
