How to sort categories by weight of posts when using GroupByParam

I have a blog sectioned into 3 sections. Each post has categories. I would like to show a sidebar on each posts that shows the categories and posts under it belonging to that section which i am able to achieve by

{{ range .Site.Sections.[section_name].Pages.GroupByParam "categories" }}
  {{ .Key }}
    {{ range sort .Pages }}
        {{ .Title }}
    {{ end }}
{{ end }}

This sorts the posts within each category.

However the categories come sorted alphabetically. Is there any way i could sort the categories based on the weight of the posts in that section i.e the category with the highest weighted post in that section should come up first.

2 Likes

Really hope anyone can help me on this