Hugo

Sorting paginated pages by date

I’m creating a content type of Events. Each event has a start and end page parameter. I haven’t been able to sort by that page parameter so I tried introducing the date variable to try to sort by that without any luck. Can you help me reverse the order of my events for my list page?

Frontmatter

---
date: 2019-05-03T09:00:00.000Z
start: 2019-05-03T09:00:00.000Z
end: 2019-05-03T12:00:00.000Z
---

Template attempts

{{ $paginator := .Paginate (where .Data.Pages.ByDate.Reverse "Section" $section) 7 }}
{{ $paginator := .Paginate (where .Data.Pages "Section" $section).ByDate.Reverse 7 }}
{{ $paginator := .Paginate (sort (where .Data.Pages "Section" $section) "start" "asc") 7 }}

There are no errors in the terminal, and the order of the events never changed.

Please read Requesting Help and share your project’s code, so other may assist.

1 Like

Tried cloning your repo but it’s over 7GB so that’s not an option for me.

Try removing .Data (untested)

{{ $paginator := .Paginate (where .Pages.ByDate.Reverse "Section" $section) 7 }}
1 Like

That will be the same.

Most of the variants above all looks correct.

Give this a whirl:

{{ $pages := where .Data.Pages "Section" $section }}

{{ $sorted := $pages.ByParam "start" }}
reverse:
{{ $sorted := ($pages.ByParam "start").Reverse }}

{{ $paginator := .Paginate $sorted 7 }}

{{ $sorted := sort $pages "Params.start" "asc" }}

also seems to work.

1 Like

It turned out Hugo was rendering the page with a different template than I thought it was using…

Once that was straightened out I used @pointyfair suggestion with success.

{{ $pages :=  where .Site.Pages "Section" $section }}
{{ $paginator := .Paginate $pages 7 }}

ul.card-list
  {{ range sort $paginator.Pages "Params.start" "asc" }}
    {{ .Render "summary" }}
  {{ end }}

{{ partial "pagination.html" . }}

You nees to sort them before you paginate them.

1 Like

If I try to use this solution in _default/list.html template I get undefined variable "$section".

Is there anything I am missing here?