# Sort map by values

**URL:** <https://discourse.gohugo.io/t/sort-map-by-values/41605>\
**Category:** support\
**Created:** [November 24, 2022, 5:26pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605 "2022-11-24T17:26:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlgo11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/carlgo11/32/18247_2.png) [@Carlgo11](https://discourse.gohugo.io/u/Carlgo11)\
**Post date:** [November 24, 2022, 5:26pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/1 "2022-11-24T17:26:45Z")

</div>

Hello,  
I have a country hash map where the keys are the respective iso-3166 alpha-2 codes and values are the common names of the countries. When trying to iterate through the hashmap Hugo sorts by the keys. How do I sort by value on a map?

Example code:

```nohighlight
---
au: Australia
ch: Switzerland
gb: United Kingdom
kr: South Korea
---

```

```nohighlight
{{ range $id, $name := .Site.Data.countries }}
  <a href="{{ $id }}">{{ $name }}</>
{{ end }}

```

Desired result:

- Australia
- South Korea
- Switzerland
- United Kingdom

Current result:

- Australia
- Switzerland
- United Kingdom
- South Korea

---

<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:** [November 24, 2022, 7:11pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/2 "2022-11-24T19:11:20Z")

</div>

Use the `value` keyword. Examples:

```auto
{{ range $id, $name := sort .Site.Data.countries "value" }} <-- default sort is ascending
{{ range $id, $name := sort .Site.Data.countries "value" "asc" }}  
{{ range $id, $name := sort .Site.Data.countries "value" "desc" }}

```

---

<div class="post-metadata">

**Author:** ![Carlgo11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/carlgo11/32/18247_2.png) [@Carlgo11](https://discourse.gohugo.io/u/Carlgo11)\
**Post date:** [November 24, 2022, 9:07pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/3 "2022-11-24T21:07:37Z")

</div>

Hmm that sorts the countries by name but now `$id` represents the loop iteration and not the hash key

---

<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:** [November 24, 2022, 9:25pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/4 "2022-11-24T21:25:29Z")

</div>

Sorry about that. I was looking at values only.

Sometimes it’s easier to visualize the data as JSON (the `jsonify` function can help with this). Your data is currently a single object with four keys. It is not an array.

```json
{
  "au": "Australia",
  "ch": "Switzerland",
  "gb": "United Kingdom",
  "kr": "South Korea"
}

```

In this case, the `sort` function discards the keys, leaving you with an array

```json
[
  "Australia",
  "South Korea",
  "Switzerland",
  "United Kingdom"
]

```

I suggest you restructure your data as an array of objects:

```yaml
- id: au
  name: Australia
- id: ch
  name: Switzerland
- id: gb
  name: United Kingdom
- id: kr
  name: South Korea

```

In JSON that looks like:

```json
[
  {
    "id": "au",
    "name": "Australia"
  },
  {
    "id": "ch",
    "name": "Switzerland"
  },
  {
    "id": "gb",
    "name": "United Kingdom"
  },
  {
    "id": "kr",
    "name": "South Korea"
  }
]

```

Then you can do:

```auto
{{ range sort .Site.Data.countries2 "name" }}
  <a href="{{ .id }}">{{ .name }}</a>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![Carlgo11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/carlgo11/32/18247_2.png) [@Carlgo11](https://discourse.gohugo.io/u/Carlgo11)\
**Post date:** [November 24, 2022, 9:36pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/5 "2022-11-24T21:36:53Z")

</div>

That worked!  
Funnily enough that’s actually how the YAML file looked initially today 🙃

---

<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:** [November 26, 2022, 9:37pm UTC](https://discourse.gohugo.io/t/sort-map-by-values/41605/6 "2022-11-26T21:37:44Z")

</div>

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