How can I conditionally sort my blog posts based on the sort query parameter in the URL?

I have the following Hugo code to paginate and sort my blog posts by date:

{{ $paginator := .Paginate (sort (where .Site.RegularPages "Section" "blog") "Date") }}
{{ $paginator := .Paginate (sort (where .Site.RegularPages "Section" "blog") "Date").Reverse }}
{{ range $paginator.Pages }}
   {{ .Title }}
{{ end }}

My website URLs look like this:

  • http://localhost:1313/en/blog/?sort=asc
  • http://localhost:1313/en/blog/?sort=desc

How can I dynamically compare the query parameter sort (either asc or desc) in the URL and display the blog posts in the correct order (asc or desc) based on this query parameter?

Why are you paginating twice?

You cannot. Hugo is a static site generator.