# Stop Hugo from sorting data (from data files) alphabetically

**URL:** <https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942>\
**Category:** support\
**Created:** [June 10, 2017, 8:08pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942 "2017-06-10T20:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianmuscat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ianmuscat/32/6575_2.png) [@ianmuscat](https://discourse.gohugo.io/u/ianmuscat)\
**Post date:** [June 10, 2017, 8:08pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942/1 "2017-06-10T20:08:53Z")

</div>

Hi,

I’ve managed to get some data to show up from a [data file](https://gohugo.io/extras/datafiles#the-data-folder), but Hugo seems to be sorting my data alphabetically, which is not what I want to do.

here’s an example of some data (YAML):

```auto
b:
  bar: 0
  baz: 1

a:
  bar: 2
  baz: 3

```

And this is an example of the template I have

```auto
{{ range $.Site.Data.foo }}
  {{ range . }}
    {{ .bar }}
  {{ end }}
{{ end }}

```

Instead of showing the data from `b` (i.e. where `bar` is `0`) first (even though it’s first), it sorts alphabetically, and shows data from `a` instead (i.e. where `bar` is `2`).

How can I modify this behaviour to get Hugo to render data in the order that it shows up in my data file? I tried to look up documentation on sorting, I did find some reference to [`sortByField`](https://gohugo.io/templates/functions#sort), but I have no idea how or where to use it; or if it could even be used with data files at all (the docs seem to mention `sortByField` exists, but not much else).

Looking forward to any ideas.

Thanks,  
Ian

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 10, 2017, 8:14pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942/2 "2017-06-10T20:14:35Z")

</div>

In short: If what you define is a dictionary/map, the keys’ order will be undefined. Nothing to do about other than sorting it (there are a sort func in Hugo that may help you).

But I do suspect that you would be better off with reorg or your data file to make it an ordered list, pseudo YAML:

```auto
foo:
- name: a
  bar: 0
  baz: 1

- name: b
  bar: 2
  baz: 3

```

Note that I’m not sure the above is even valid YAML, but I hope the message is clear.

---

<div class="post-metadata">

**Author:** ![ianmuscat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ianmuscat/32/6575_2.png) [@ianmuscat](https://discourse.gohugo.io/u/ianmuscat)\
**Post date:** [June 10, 2017, 8:20pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942/3 "2017-06-10T20:20:34Z")

</div>

Whoa that reply was fast!

I forgot to mention that I did try what you mentioned, but I get the following error in Hugo when I do:

```auto
line 1: cannot unmarshal !!seq into map[string]interface {}

```

Perhaps I need to change my template to fit the new data structure somehow?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 10, 2017, 8:25pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942/4 "2017-06-10T20:25:45Z")

</div>

> [@ianmuscat](#):
>
> Perhaps I need to change my template to fit the new data structure somehow?

My memory is a little faded here, but I suspect you try to make sure you have a “root” in your YAML file so it is just not that list. But it is hard to tell without seeing the actual project.

---

<div class="post-metadata">

**Author:** ![ianmuscat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ianmuscat/32/6575_2.png) [@ianmuscat](https://discourse.gohugo.io/u/ianmuscat)\
**Post date:** [June 10, 2017, 8:51pm UTC](https://discourse.gohugo.io/t/stop-hugo-from-sorting-data-from-data-files-alphabetically/6942/5 "2017-06-10T20:51:43Z")

</div>

Your suggestion of adding a root element to the YAML file seems like it could work, the loop works, however no data would actually show up (probably because I need to change something in my template to reflect the new data structure).

Since this was getting a bit too complex, I simply decided to work around it by naming the YAML key names to `0`, `1`, `2`… not the most elegant solution, but I guess it works well.

Thanks for your help.
