Pages.GroupBy "Weight" doesn't put 0 weight group last

Unlike Pages.ByWeight, which puts pages with weight 0 last.

It should either match ByWeight, or we should have a Pages.GroupByWeight that matches Pages.ByWeight by putting the weight 0 group last.

Thinking out loud…

Sorting by weight

Zero/nil weights on pages, menu entries, etc. do not fall between negative and positive values when sorted. For example, with Drupal, weights sort like this:

-2, -1, 0, 1, 2

Lighter items float to the top, while heavier items sink to the bottom.

With Hugo, weights sort like this:

-2, -1, 1, 2, 0/nil

I’m not particularly enamored with this behavior, but that ship sailed a decade ago.

Grouping by weight

When grouping by weight we do it this way:

-2, -1, 0/nil, 1, 2

Which, to me, is the right way to do it.


I can’t think of a great way to change this behavior without breaking a lot of sites, and adding another method is just ugly.


Yeah, it’s not terribly pretty…

{{ $p := site.RegularPages }}
{{ $zeroWeightPages := where $p "Weight" 0 }}
{{ $nonZeroWeightPages := $p | complement $zeroWeightPages }}

{{ $groups := $nonZeroWeightPages.GroupBy "Weight" | append (dict "Key" 0 "Pages" $zeroWeightPages) }}

{{ range $groups }}
  <h3>{{ .Key }}</h3>
  {{ range .Pages }}
    <h4><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h4>
  {{ end }}
{{ end }}

Example of current behaviors and workaround

git clone --single-branch -b hugo-forum-topic-50218 hugo-forum-topic-50218
cd hugo-forum-topic-50218
hugo server