Sort order only ByDate or ByWeight

Having been a Lotus Notes/Domino developer for almost two decades my love in views seems to be in-curable. As Notes is dying, the hunt for an alternative has set in. Fortunately Hugo seems to offer a lot of similar features. Even categorized views, which is implemented in my new website migrated from Lotus Notes inspired by a comment from tryjigs.

Now I try to get my head around switching sort order for documents with the same term. Just like the option to sort a search result in this forum by date, relevance etc.

I understand the concept of nodes in Hugo the way, that a node is a node is a node. Documents with a certain term can only be listed in one way (they are born with only one route). If I’m not wrong here, I have to hack a little and put the same term in two different taxonomies in order to create two templates with different sort order of the same document collection.

So far so good.

Next challenge is, that I’m going for a pure “ByDate.Reverse” layout and a pure “ByWeight” layout of the same documents. In order to accomplish that, “weight” has to be present in front matter.

But this is not good - at least according to the docs:

By default, content is ordered by weight, then by date with the most recent date first, but alternative sorting (by title and linktitle) is also available.
Unweighted pages appear at the end of the list. If no weights are provided (or if weights are the same), date will be used to sort. If neither is provided, content will be ordered based on how it’s read off the disk, and no order is guaranteed.

As I read it, a node sorted “ByDate” will always be influenced by the documents weight - if present in front matter. And I need that to be the case, as weight is required for the other node, which is intended to be sorted by weight.

Have I missed something?

If not, then I would like to suggest a consequent separation of “ByDate” and “Weight” in nodes as a feature request.

The documentation is wrong. ByDate uses Date only to sort. But I’m not sure how sorting and taxonomy pages work (I see a TODO in the code here now).

1 Like

Would really not like to be too stubborn here, but:

This node

contains the following code:

{{ range .Paginator.Pages.ByDate.Reverse -}}
{{ partial "summaryList.html" . }}
{{ end }}
</div>
{{ partial "pagination" . }}

which - I hoped - would deliver a newest-post-first node. But first page begins with a 2013 post. And if you go to page 2 (“Næste side”), you bump into a 2014-post.

Either is ByDate “polluted” by something, or my range/pagination code is wrong.

{{ range .Paginator.Pages.ByDate.Reverse -}}

You are right in the above example, I was thinking about “something else” – that will work on “any node”, including the taxonomy lists. But note that the paginated example above is probably not what you want, as it will sort the current page and not the entire page set. Something like this would probably be more correct if you want pagination:

{{ range .Paginate(.Pages.ByDate.Reverse).Pages -}}

Dooh. Dammit.

You are absolutely right. I’ve stubled on those .Paginator .Paginate etc. things.

My Hugo demands more parentheses, though. This syntax works like a charm:

{{ range (.Paginate (.Pages.ByDate.Reverse)).Pages -}}

Thank you for your assistance. You saved my day.

…And the feature request is hereby withdrawn.

We allready got total separation of ByDate and ByWeight sort orders, and it even works!

1 Like

I’d like to add here for posterity:

{{ range $index, $element := .Paginator.Pages.ByDate }}

This sorts posts be newest first.

1 Like