How to Sort Sections

The structure of my website is like this

blog
├── funny-cats
│   └── kittens
│    |   └── _index.md
│   └──katten
          └── _index.md
├── funny-dogs
│   └── dogs
│    |   └── _index.md
│   └──honden
          └── _index.md

I want to sort the subsections in every Home sections. The version I list below only list the subsections but not also sort them. Anybody has similar requirement before? Thanks.

{{ $sectionname := .Title}}
{{ range .Sections }}
<li>			
<h5>{{ .Title }}</h5>
{{ range first $.Site.Params.lenPagesInSectionList .Pages }}
<a href="{{.RelPermalink}}">{{.Title}}</a>
{{ end }}

{{ if gt (len .Pages) $.Site.Params.lenPagesInSectionList }}
<a href="/">Read More <span class="glyphicon glyphicon-share-alt"></span></a>
{{ end }}
{{end}}
</li>
{{end}}             

How should I add sorting in {{ range .Sections }}?

{{ range .Sections.ByTitle }}

Hello bep,
Thanks. Do you know how I could assign the weight of sections and then sort them by the weight?

You need to add content files with front matter and weight to the sections (i.e. the “_index.md” type).

You are right. After assigning weight, you even don’t need use {{range .Sections.ByWeight}} becauseju {{ range .Sections }} listed the sections in the order of the weight.

2 Likes