# How to sort data file content by date in reverse?

**URL:** <https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552>\
**Category:** support\
**Created:** [January 10, 2021, 8:58pm UTC](https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552 "2021-01-10T20:58:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akronym\_Rising](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/akronym_rising/32/3769_2.png) [@Akronym\_Rising](https://discourse.gohugo.io/u/Akronym_Rising)\
**Post date:** [January 10, 2021, 8:58pm UTC](https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552/1 "2021-01-10T20:58:44Z")

</div>

I have a collection of yaml data files, (one file per week) using the below format. I’m able to display the data, for example here: [http://class.ronliskey.com/class/6/geology/](http://class.ronliskey.com/class/6/geology/)

The data is listed by week. How can I display these weeks in reverse order? The following ranges work, but I’m unable add any form of sort. I would like to sort by either “week\_starts” or “week”.

`{{- range $data -}}`

`{{- range $index, $element := $data -}}`

**Example YAML Data File**

```
  ---
  week: 22
  week_starts: 2020-01-11
  grade: 6
  bid: geol
  block: "Geology"
  code: geol6
  display: true
```

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 10, 2021, 9:09pm UTC](https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552/2 "2021-01-10T21:09:31Z")

</div>

> [@Akronym\_Rising](#):
>
> The data is listed by week. How can I display these weeks in reverse order?

Try something like:

```auto
{{- $data := sort .Site.Data.<name-of-folder-containing-data-files> ".week" "desc" -}}
{{- range $index, $element := $data -}}
...
{{- end -}}

```

More about: [collections.Sort | Hugo](https://gohugo.io/functions/sort/#readout)

---

<div class="post-metadata">

**Author:** ![Akronym\_Rising](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/akronym_rising/32/3769_2.png) [@Akronym\_Rising](https://discourse.gohugo.io/u/Akronym_Rising)\
**Post date:** [January 10, 2021, 9:25pm UTC](https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552/3 "2021-01-10T21:25:37Z")

</div>

Thanks for the fast reply. When I try that I get:

`ERROR 2021/01/10 13:20:02 render of "page" failed: execute of template failed: template: class/single.html:49:30: executing "class/single.html" at <partial "subjects/schedule.html" .>: error calling partial: "/Volumes/Passport4T/Sites/test_hugo/ronliskey_class/layouts/partials/subjects/schedule.html:18:27": execute of template failed: template: partials/subjects/schedule.html:18:27: executing "partials/subjects/schedule.html" at <.Site.Data.c2023.g6.geology>: can't evaluate field Site in type bool`

I tried adding $ to .Site

```
{{- $data := sort $.Site.Data.c2023.g6.geology ".week" "desc" -}}
{{- range $index, $element := $data -}}

```

and got:

`ERROR 2021/01/10 13:21:17 render of "page" failed: execute of template failed: template: class/single.html:49:30: executing "class/single.html" at <partial "subjects/schedule.html" .>: error calling partial: "/Volumes/Passport4T/Sites/test_hugo/ronliskey_class/layouts/partials/subjects/schedule.html:18:17": execute of template failed: template: partials/subjects/schedule.html:18:17: executing "partials/subjects/schedule.html" at <sort $.Site.Data.c2023.g6.geology ".week" "desc">: error calling sort: sequence must be provided`

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 11, 2021, 6:23am UTC](https://discourse.gohugo.io/t/how-to-sort-data-file-content-by-date-in-reverse/30552/4 "2021-01-11T06:23:10Z")

</div>

> [@Akronym\_Rising](#):
>
> error calling sort: sequence must be provided

What is the folder tree under `/data/`?  
As the error message suggests there needs to be a sequence of data files all of which must contain the `week` parameter for the sorting to work.

It seems that you need to share the project for us to have a look.
