Sorting and pagination

I have a list template that outputs items sorting them by a Date stamp (if it exists) or by Title, and paginates them.

{{ $paginator := .Paginate .Data.Pages }}
{{ range $paginator.Pages }}

I want to sort items by price. If I change the code to

{{ range $paginator.Pages.ByParam "price" }}

- it sorts on price within each page.

If I change it to

{{ range .Pages.ByParam "price" }}

- it sorts listed items according to price, but stops paginating them - they all come out on one page as much as there are.

How I can sort the whole lot of itmes and then paginate them?

Thank you!

I’m not a Hugo expert but generally you’d need to sort first then paginate. The theme I use does this like so:

{{- $sortedPages := $pages.ByParam "Title" }}

{{ $paginator := .Paginate $sortedPages }}

{{- range $index, $page := $paginator.Pages }}
2 Likes

Exactly! Thank you, I did it! If someone is interested - here’s my solution:

{{- $sortedPages := .Pages.ByParam "price" }}
{{ $paginator := .Paginate $sortedPages }}
{{- range $index, $page := $paginator.Pages }}
1 Like

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