Sort a range by custom param

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:

[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:

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

Thanks for your help.

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

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

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

You could try breaking it up to debug it

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

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.

I found the solution.

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

Thanks for your time.

1 Like