Sort grouped and sorted content based on custom slice

Hello everyone, I’m in need in support because I don’t know how to achieve this sorting.

To put you in context

  > section1/
  > >
  > > 
  > > 
  > > 
  > > 

Now, I need to make groups of posts, which are also ordered within that group.
So i ended adding a group and order param in every post*.md’s front matter I made.

What I want to achieve in the list.html of section1 is something like this:


Where all the posts are listed in order under their corresponding group, and all groups are listed in order also based in, for example, an array of groups declared maybe in

Is this possible? I was messing with ranges, sort, .GroupBy, .ByParam, etc, without success :confused:

Thank you in advance, and sorry for my english.

Yes, this is possible. For example, given the below content structure:

│   └───section-1

This template code:

{{- range .Sections }}
  <h1>{{ .Title }}</h1>
  {{- range .Pages.GroupByParam "group" }}
    <h2>{{ .Key }}</h2>
    {{- range .Pages.ByParam "order" }}
      <h3>{{ .Title }}</h3>
    {{- end }}
  {{- end }}
{{- end }}

Would produce this output:

<h1>Section 1</h1>
      <h3>Post 1</h3>
      <h3>Post 2</h3>
      <h3>Post 3</h3>
      <h3>Post 4</h3>
      <h3>Post 5</h3>

Keep in mind that each post has a group and order param. For example:

title: "Post 1"
group: A
order: 01

Thank you very much for the explanation!!!
I’ve already encountered that, but there’s two thing I need to do which the example is not covering. Maybe I’m thinking this wrong, so I’ll be glad if you could clear things a little for me.

order’s ok, it could be just a number so sorting them numerically/alphabetically will be the same and its correct.
As for groups, I’ll need to sort them based on a slice. And also will be perfect if only posts from groups present on the array are listed (as a safe-check).

Take into account I have a param defined in , groups: ["B", "D", "A", "C"]
I want to list groups in that order

This is untested, but maybe you could do something like:

<!-- Get the list of valid groups -->
{{ $page := .Site.GetPage "/section-1/" }}
{{ $groups := $page.Params.groups }}


<!-- Only list pages where group param is in groups list -->
{{ if in $groups }}
{{ end }}

Ok zwbetz, thanks to your help I think i’ve got it!
Tell me what you think, If there’s sth to improve or something that should’t be do it this way:

          {{ range .Params.groups }}
            <h2>{{ . }}</h2>
            {{ range where ($.Pages.ByParam "order") "" . }}
            {{ end }}
          {{ end }}

This appears to be very similar to what I am trying to do.

Given the following directory structure:

├── abel
│   ├── headshot_grayscale.jpg
│   └──
├── acel
│   ├── acel.png
│   └──
├── cmac
│   ├── chris2.jpg
│   └──
├── csam
│   ├──
│   └── csam-grayscale.jpg
├── ctil
    ├── ctil.png

The following for front matter:

title: "Chris Til"
affiliation: ["PhD Student"]
sort_order: "6"
draft: false
featured_image: "ctil.png"

The goal is to have a members page that lists lab members by affiliation (sorted alphabetically).

Affiliations must be sorted in a certain order though (not alpha), thus we have the need of the ‘sort order’ numerical hey.

{{ range .Pages.ByParam "sort_order" }

provides the correct listing of members (but without the ‘affiliation’ title).

Do you have any suggestions for how I can display the ‘affiliation’ over each group (already ordered by ‘sort order’)?

Many thanks (in advance) for any assistance

I have tried several of the suggestions from other posts but have not been able to achieve the objective.

arbitrarily ordered groups of page images

I can sort all the member pages in the correct order, by sorting on the sort_order. However, I can’t get a nice list of affiliations to place a header over each group.

{{ $affiliation := .Pages.Params.affiliation }}
{{ $affiliation }}


execute of template failed: template: members/list.html:5:25: executing "content" at <.Pages.Params.affiliation>: can't evaluate field Params in type page.Pages

Taking the approach listed above, I tried to loop over the GroupByParam “affiliation” but nothing is output (perhaps I have not set up sections)?

Almost got the solution!!!

  {{- range .Pages.GroupByParam "sort_order" }}
    <h2>{{ .Key }}</h2>
    {{- range .Pages.ByParam "affiliation" }}
      <h3>{{ .Title }}</h3>
    {{- end }}
  {{- end }

Displays the correct order, with headers over each group! However, the header that is displayed is the sort_order, not the affiliation.

Suggestions on how to get the affiliation to display?

Even this hack-ish solution does not avail:

    <h2>{{ .Key }}</h2>
    {{ if eq .Key 1 }}
    {{ end }}

there is only a single


This is the hack solution:

    {{ if eq .Key "1" }}
    {{ end }

and it works for now.