Is there a possibility to use Sort on .Paginate?

Hello everybody,

Can we use function sort on Paginate.

I’ve tried

{{ $paginator := .Paginate ((where .Data.Pages "Type" "foo") sort .Data.Pages "Type" "foo" ".Params.dateAdd" "desc") 51 }}
{{ range $paginator.Pages }}

No errors but no sorting

Thanks.

That function call looks wrong. Shouldn’t it be something like (untested)

{{ $paginator := .Paginate (sort (where .Data.Pages "Type" "foo") "Type" "foo" ".Params.dateAdd" "desc") }}

Note how there is only a single reference to .Data.Pages, and the value returned by the where call is passed to the sort function as the first argument.

Thank you but unfortunately it doesn’t work

Doesn’t work in what sense? Do you get an error message? The content is output but unsorted? There is no output?

First of all, thank you for taking time off my problem. :smiley:

My error message

executing "main" at <.Paginate>: error calling Paginate: unsupported type in paginate, got []*hugolib.Page

This seems like a bug to me… Paginate should really accept a []*hugolib.Page. It would be great if you could file that. If you have a minute it would be great if you could file it.

In the meantime, you can get around it with something like this:

{{ $uselessPaginator := .Paginate .Data.Pages }}
{{ range $page := last $uselessPaginator.NumberOfElements (first (sub (mul $uselessPaginator.PageNumber $uselessPaginator.PageSize) 1) $pages) }}
  Do stuff here
{{ end }}

The idea is that you create a paginator just to get Hugo to generate the pages, but instead of actually using it to output data you use your own array ($pages can be whatever you want), sorted however you please. It’s a bit of a hack, but not too bad.