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

content/
  > section1/
  > > _index.md
  > > post1.md 
  > > post2.md 
  > > post3.md 
  > > post4.md 

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:

<h1>Section1</h1>
  <h2>Group1</h2>
    <h3>Order1</h3>
    <h3>Order2</h3>
  <h2>Group2</h2>
    <h3>Order1</h3>
    <h3>Order2</h3>
    <h3>Order3</h3>

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 _index.md.

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:

├───content
│   └───section-1
│           post-1.md
│           post-2.md
│           post-3.md
│           post-4.md
│           post-5.md
│           _index.md

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>
    <h2>A</h2>
      <h3>Post 1</h3>
      <h3>Post 2</h3>
    <h2>B</h2>
      <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 _index.md , 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/_index.md" }}
{{ $groups := $page.Params.groups }}

...

<!-- Only list pages where group param is in groups list -->
{{ if in $groups .Params.group }}
  ...
{{ 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") "Params.group" . }}
              <h3>{{.Title}}<h3>
            {{ end }}
          {{ end }}

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

Given the following directory structure:

content/members  
├── abel
│   ├── headshot_grayscale.jpg
│   └── index.md
├── acel
│   ├── acel.png
│   └── index.md
├── cmac
│   ├── chris2.jpg
│   └── index.md
├── csam
│   ├── index.md
│   └── csam-grayscale.jpg
├── ctil
    ├── ctil.png
    └── index.md

The following for front matter:

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

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 }}

produces:

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 }}
        <h2>Faculty</h2>
    {{ end }}

there is only a single

<h2>1</h2>

Solved
This is the hack solution:

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

and it works for now.