Ordering data by custom weight parameter

Have been using Hugo for few weeks on and off. Now trying it for a live project. Just having a hiccup trying to sort list of docs by custom weight parameter. Any of you had similar requirement before? Just wondering how you solved it.

Front matter in the question is below and what we are trying to achieve is to list all documents by
group and then docs. Sorting should be based on ‘weight’ parameter. Any thoughts?

Expected result:

Business Docs
  Invoices
  Expenses

Personal Docs
  Resume
  Bookmarks

Thanks for your time.

---
title: Document List
type: doc
documents:
  - docs:
      - title: "Resume"
        weight: '100'
      - title: "Bookmarks"
        weight: '95'
    group: Personal Docs
    weight: '90'
  - docs:
      - title: "Invoices"
        weight: '100'
      - title: "Expenses"
        weight: '95'
    group: Business Docs
    weight: '100'
---

You need to elaborate further and provide more data. What exactly are you trying to achieve?

add .ByWeight to the end of the query. i.e.

{{ range $key, $value := .Data.Taxonomy.ByWeight }}

Answering my own question. We finally managed to get this working. Solution below.

{{ range $documents := sort ($.Params.Documents) "weight" "desc" }}
        <ul>
            {{ .group }}
            {{ range $docs := sort ($documents.docs) "weight" "desc" }}
                <li>{{ .title }}</li>
            {{ end }}
        </ul>
{{ end }}

Ref: Order data files by secondary parameter

1 Like

Taxonomies already have their own weight that is used with the convention of taxonomyname_weight.