The resulting groups are sorted by the grouping field. For example, if you group by expiration date, the pages within each group are sorted by expiration date. Although not explicitly documented, this is what I would expect, similar to SQL’s “GROUP BY” clause.
To sort the pages in each group (e.g., by weight):
{{ range .Pages.GroupByExpiryDate "2006-01" }}
<p>{{ .Key }}</p>
<ul>
{{ range .Pages.ByWeight }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
{{ end }}
The problem here is that we are inconsistent. GroupBy and GroupByParam retain the original sort order order (which may or may not be the default sort order) in each group, but the five date grouping functions do not.
To clarify, if you sort a page collection by Title in descending order, the pages in each group are still sorted by Title in descending order after using the GroupBy or GroupByParam methods.
{{ $p := sort site.RegularPages "Title" "desc" }}
{{ range $p.GroupByParam "myParam" }}
<h2>{{ .Key }}</h2>
{{ range .Pages }}
<h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}
{{ end }}
Although a breaking change (albeit with limited exposure), I think we should change the Group By date methods to behave like GroupBy and GroupByParam.
So, I’m waffling on this. Sorting the pages of each group by the GroupBy date is, I think, the least surprising, and you can change the sorting within the groups as I’ve shown in my initial response.
I’m not sure there’s a right answer here, but leaving things as they are (a) won’t break existing sites, (b) is consistent with SQL behavior, and (c) is a little bit better from a performance standpoint (we don’t have to iterate twice over the page collection).
I think the current behavior is fine, for the reasons you listed, if we add a .Pages.ByDefaultOrder so it’s clear how to restore the default order within a .GroupBy* result (with the documentation now explaining that the default order is .Pages.ByDefaultOrder).
@jmooring Now that I look, I don’t see a way to manually recreate the default order: GroupByWeight, GroupByTitle, and GroupByFilePath don’t exist.
Your example groups by expiry date, then orders by weight, but that isn’t the default order, because pages with equal weight won’t have a suborder for title or file path.
To get the default order, you’d need to get the list of unpinned pages, then group by year/month date, then for each date group, group by weight, then for each weight group, group by title, etc. How can I do that?