Sort sub section by most recent leaf

Hi everyone,
I’m having posts on my blog, and some posts are grouped inside a branch. When listing the elements for displaying a list, I’m listing only first level leafs mixed with first level branches.
(Things are just a bit more complicated because there’s a “year” level between blog and posts, but I don’t think it is usefull to add this complexity here)

Eg for this stucture:

content
  |_ blog
      |_ post1.md
      |_ post2.md
      |_ branch1
      |   |_ _index.md
      |   |_ branch1post1.md
      |   |_ branch1post2.md
      |_ post3.md

I’m listing post1, post2, branch1, post3

branch1 is included in my list with the caracteristics of its _index.md file, wich has a date in its front matter. So when sorting my listing by date, branch1 is sorted looking at that front matter date.

But this is not what I want: I would like to look at the date of branch1 latest leaf (eg the date of branch1post2, not at the date in the _index.md file

At this time, I’m doing it manually: I change the date in front matter of _index.md to reflect the date of the latest post in the branch. But this is not a clean way to do it : it is tedious and error prone, and if collaborating with people to manage posts, I have to explain this to them.

I did not see a “Page” variable or function that I can use for sorting things : I mean, something returning

  • the date of the latest leaf in branch if page is a branch,
  • the date of the page itself if page is a leaf.

Can anybody help me solve this problem ?