# How to sort a map by multiple params

**URL:** <https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628>\
**Category:** support\
**Created:** [October 11, 2023, 2:33pm UTC](https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628 "2023-10-11T14:33:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![totoff](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/totoff/32/2286_2.png) [@totoff](https://discourse.gohugo.io/u/totoff)\
**Post date:** [October 11, 2023, 2:33pm UTC](https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628/1 "2023-10-11T14:33:43Z")

</div>

Dear all,

I have a map of team members which I need to sort 1. alphabetical by family name and 2. by an integer assigned to each item. How can I do that when ranging over the map?

Many thanks!

---

<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 11, 2023, 4:24pm UTC](https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628/2 "2023-10-11T16:24:26Z")

</div>

Assuming a data structure similar to:

> **data/team.json**
>
> ```plaintext
> [
> {
> "last_name": "Smith",
> "first_name": "John",
> "some_value": 2
> },
> {
> "last_name": "Smith",
> "first_name": "Robert",
> "some_value": 5
> },
> {
> "last_name": "Smith",
> "first_name": "Marcia",
> "some_value": 1
> },
> {
> "last_name": "Jones",
> "first_name": "Kevin",
> "some_value": 7
> },
> {
> "last_name": "Jones",
> "first_name": "Timothy",
> "some_value": 6
> },
> {
> "last_name": "Jones",
> "first_name": "Patricia",
> "some_value": 42
> }
> ]
> 
> ```

Build a unique, sorted slice of the last names, then range through the slice, using a where clause to get matching persons.

```plaintext
{{ $lastNames := slice }}
{{ range site.Data.team }}
  {{ $lastNames = $lastNames | append .last_name }}
{{ end }}
{{ $lastNames = $lastNames | uniq | sort }}

{{ range $lastNames }}
  {{ range sort (where site.Data.team "last_name" .) "some_value" }}
    <pre>
      First name: {{ .first_name }}
      Last name: {{ .last_name }}
      Some value: {{ .some_value }}
    </pre>
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![razon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/razon/32/14882_2.png) [@razon](https://discourse.gohugo.io/u/razon)\
**Post date:** [October 11, 2023, 4:27pm UTC](https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628/3 "2023-10-11T16:27:44Z")

</div>

Alternatively, you can group the members via [scratch](https://gohugo.io/functions/scratch/).

```go
{{ $m := slice
  (dict "fn" "Fizz" "age" 30)
  (dict "fn" "Foo" "age" 20)
  (dict "fn" "Foo" "age" 17)
  (dict "fn" "Bar" "age" 11)
  (dict "fn" "Bar" "age" 22)
  (dict "fn" "Fizz" "age" 8)
}}
{{ $data := newScratch }}
{{/* sorting by age */}}
{{ range sort $m "age" }}
  {{/* group by fn */}}
  {{ $data.Add .fn (slice .) }}
{{ end }}
{{ range $data.Values }}
  {{ range . }}
    {{ warnf "fn: %s, age: %d" .fn .age }}
  {{ end }}
{{ end }}

```

Result:

```plaintext
WARN fn: Bar, age: 11
WARN fn: Bar, age: 22
WARN fn: Fizz, age: 8
WARN fn: Fizz, age: 30
WARN fn: Foo, age: 17
WARN fn: Foo, age: 20

```

---

<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, 2023, 4:28pm UTC](https://discourse.gohugo.io/t/how-to-sort-a-map-by-multiple-params/46628/4 "2023-10-13T16:28:03Z")

</div>

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