# Sort grouped and sorted content based on custom slice

**URL:** <https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469>\
**Category:** support\
**Created:** [January 18, 2019, 3:59pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469 "2019-01-18T15:59:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discourse.gohugo.io/u/blez)\
**Post date:** [January 18, 2019, 3:59pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/1 "2019-01-18T15:59:21Z")

</div>

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

To put you in context

```auto
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:

```auto
<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 😕

Thank you in advance, and sorry for my english.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [January 18, 2019, 6:41pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/2 "2019-01-18T18:41:33Z")

</div>

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

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

```

This template code:

```nohighlight
{{- 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:

```nohighlight
<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:

```nohighlight
---
title: "Post 1"
group: A
order: 01
---

```

---

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discourse.gohugo.io/u/blez)\
**Post date:** [January 18, 2019, 7:29pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/3 "2019-01-18T19:29:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [January 18, 2019, 7:42pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/4 "2019-01-18T19:42:27Z")

</div>

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

```nohighlight
<!-- 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 }}

```

---

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discourse.gohugo.io/u/blez)\
**Post date:** [January 18, 2019, 9:44pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/5 "2019-01-18T21:44:52Z")

</div>

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:

```auto
          {{ range .Params.groups }}
            <h2>{{ . }}</h2>
            {{ range where ($.Pages.ByParam "order") "Params.group" . }}
              <h3>{{.Title}}<h3>
            {{ end }}
          {{ end }}

```

---

<div class="post-metadata">

**Author:** ![noah-de](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/noah-de/32/10002_2.png) [@noah-de](https://discourse.gohugo.io/u/noah-de)\
**Post date:** [February 27, 2020, 6:45pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/6 "2020-02-27T18:45:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noah-de](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/noah-de/32/10002_2.png) [@noah-de](https://discourse.gohugo.io/u/noah-de)\
**Post date:** [February 27, 2020, 7:07pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/7 "2020-02-27T19:07:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![noah-de](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/noah-de/32/10002_2.png) [@noah-de](https://discourse.gohugo.io/u/noah-de)\
**Post date:** [February 27, 2020, 7:22pm UTC](https://discourse.gohugo.io/t/sort-grouped-and-sorted-content-based-on-custom-slice/16469/8 "2020-02-27T19:22:47Z")

</div>

> [@noah-de](#):
>
> Displays the correct order, with headers over each group! However, the header that is displayed is the _sort\_order_ , not the _affiliation_ .

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.
