How to maintain order when I have subdirectories?

In the https://mfaani.com/posts/devtools/ directory I have 5 posts and one subdirectory of https://mfaani.com/posts/devtools/optimizing-app-size/.

The 5 posts are sorted by date. But the weight nor Date frontmatter fields within the _index.md of the /optimizing-app-size directory isn’t factored in the ordering. The subdirectory just gets dumped at the end of the list.

When I attempt to change the weight of a single post within the /devtools directory, things work though.

This my https://github.com/mfaani/mfaani/tree/main/content/posts/devtools

That’s the way the papermod theme builds its page collection:

https://github.com/adityatelange/hugo-PaperMod/blob/master/layouts/_default/list.html#L41 Override the template and sort the collection.

1 Like

Thank you so much. So the pages are sorted, but the combined $pages variable isn’t. Because I have ‘sorted pages, followed by sections’ which just get dumped? That makes perfect sense.

I ended up with the following:

{{- $pages := union .RegularPages .Sections}}
{{ $sortedPages := sort $pages "Params.weight"  "Params.date"}}

The signature of the sort function is:

sort COLLECTION [KEY] [ORDER]

So this doesn’t make any sense:

sort $pages "Params.weight"  "Params.date"}}

https://gohugo.io/functions/sort/

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.