This post from 2015 suggests that it’s not possible to sort by arbitrary frontmatter variables. Since it’s been two years, I was wondering if that’s still the case.
I understand that weight can be used to resolve this by generating the appropriate order, but that’s not particularly user-friendly and it discards important semantics that are already present in the front-matter.
Because this method takes a parameter, that presumably means your sort invocation in the template would have to change a little. For example, sorting your recipes by date looks like this:
{{ range where ((sort .Site.Pages.ByDate) "Section" "recipe") }}
but maybe sorting your recipes by their star rating looks like this:
{{ range where ((sort (.Site.Pages.ByFrontmatter "rating")) "Section" "recipe") }}
I’m not an experienced Go developer so I wouldn’t mind a sanity check on this. Is this a plausible approach? If so I’d be willing to submit a PR.
Prior to this PR, you could only sort using mechanisms that were explicitly provided: sort by date (.ByDate), sort by title (.ByTitle), and so on. But what if you wanted to sort by some arbitrary frontmatter key? Maybe you have a bunch of recipes and they all have a zero to five star rating, indicated by a key named rating. Hugo had no way of understanding custom key-based sorts like this.
Now you can sort by any arbitrary key that might exist in your frontmatter, using the ByParam sort introduced in this PR. Does that make sense?
You decide! It’s based on the value of whichever frontmatter parameter you specify should be determining the sort order.
[quote=“36928495, post:5, topic:5358, full:true”]
This is a great addition.[/quote]
Thank you for the kind words!
Short answer: yes!
Long answer: Originally Hugo would have interpreted this as a top-level key named menu.main.weight, rather than a top-level key named menu, pointing to a map with a key named main, pointing to a map with a key named weight. However, I also wrote a different PR that addressed this, and Hugo now understands nested parameter references as arguments to Param and similar methods: