# Sort a range by custom param

**URL:** https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382
**Category:** support
**Created:** [March 4, 2019, 7:44pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382 "2019-03-04T19:44:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![TheQuant](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thequant/32/7895_2.png) [@TheQuant](https://discourse.gohugo.io/u/TheQuant)
#### Post date: [March 4, 2019, 7:44pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/1 "2019-03-04T19:44:07Z")

</div>

I use the following code to display 6 projects (the code is from the academic-theme).

Per default it is sorted by the date field:

```
{{ range $idx, $item := first 6 (where $.Site.RegularPages "Type" ($page.Params.folder | default "project")) }}
        {{ $link := $item.RelPermalink }}

...
{{ end }}

```

I added a custom parameter to each item:

```auto
[Params]
date_sort = 2018-06-24T00:00:00

```

Now I want to sort this range by my new paramter “date\_sort” in descending order. How can I do this?

I tried this, but it has no effect:

```auto
{{ range $idx, $item := first 6 (sort (where $.Site.RegularPages "Type" ($page.Params.folder | default "project")) ".Params.date_sort" "desc") }}

```

Thanks for your help.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [March 4, 2019, 8:12pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/2 "2019-03-04T20:12:23Z")

</div>

We would need to see more than that code snippet to better help. Can you share your project?

---

<div class="post-metadata">

### Author: ![TheQuant](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thequant/32/7895_2.png) [@TheQuant](https://discourse.gohugo.io/u/TheQuant)
#### Post date: [March 4, 2019, 8:27pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/3 "2019-03-04T20:27:33Z")

</div>

Basically it’s the academic theme, this file in line 70:

> <https://github.com/gcushen/hugo-academic/blob/e2274c6b35d56e9cc50b7d67c96e8dbbe073182d/layouts/partials/widgets/portfolio.html>

I just added the “first 6” but now I want to do the sort as well.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [March 4, 2019, 8:35pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/4 "2019-03-04T20:35:29Z")

</div>

You could try breaking it up to debug it

```nohighlight
{{ $pages := where $.Site.RegularPages "Type" ($page.Params.folder | default "project") }}
{{ range $idx, $item := first 6 (sort $pages ".Params.date_sort" "desc") }}

```

---

<div class="post-metadata">

### Author: ![TheQuant](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thequant/32/7895_2.png) [@TheQuant](https://discourse.gohugo.io/u/TheQuant)
#### Post date: [March 4, 2019, 9:19pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/5 "2019-03-04T21:19:27Z")

</div>

Doesn’t change anything. I think the problem is, that I should write “$item.Params.date\_sort” because each item has the correct date\_sort.

But the $item is not known at this position, only inside the range.

---

<div class="post-metadata">

### Author: ![TheQuant](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thequant/32/7895_2.png) [@TheQuant](https://discourse.gohugo.io/u/TheQuant)
#### Post date: [March 4, 2019, 9:39pm UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/6 "2019-03-04T21:39:04Z")

</div>

I found the solution.

I had to remove the [Params] header in my pages. Now the sort works.

Thanks for your time.

---

<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: [February 28, 2020, 8:12am UTC](https://discourse.gohugo.io/t/sort-a-range-by-custom-param/17382/7 "2020-02-28T08:12:37Z")

</div>


