Sort two parameters inside data

Hello, everyone! I have a problem, hope someone can help me with solution, I can’t find for hours. What I need is sort range of data files by variable, and after that do another sort by another variable. I will show, what I am trying to do.

{{ range sort (sort .Site.Data.portfolio_residential “project_name”) “favorite”}}
{{ end }}

favorite: “2”
project_name: Betesh
picture: /assets/images/public/BET_200220_07.jpg
address: Brooklyn, NY

So I want to get list of favorites. Favorites could be from 1 to 7. It looks like: if from 1 to 6, they are on the top by numerous order. If “favorite” is 7, use alphabet order. Now it looks strangely. Favorites are on the top, but alphabet looks a bit wrong, also if I add one more item f.e. with favorite - “1” (so they are two with same number) on the top appears not alphabetically first…

A bit complicated description, sorry. Would be glad for any help! Thanks for your time!

To be more clear:
This sorting works. F.e. if I remove parameter “project_name”, and leave only “favorite”, the items with parameter = 7 are created randomly each time.
With both parameters, it stops creating randomly, but order is strange, mostly alphabetical, but some items are not in their places. I wondering what I am doing wrong, the second parameter is correct and checked, there are no mistakes… Still need some help, guys!

I think you are talking about a custom taxononmy for your parameter - when enabled, then you can do sorting on it

and maybe also this post

The SQL equivalent of what the OP wants to achieve is:

SELECT * FROM .Site.Data.portfolio_residential ORDER BY favorite ASC, project_name ASC;

As far as I know this is not easily achievable. When sorting pages you can use nested .ByParam calls, but not when working with data files.

You may be able adapt this to your use case, but it’s complicated:

This post from @FelicianoTech was really helpful:

data/portfolio_residential.json:

[
  {
    "favorite": "3",
    "project_name": "D"
  },
  {
    "favorite": "3",
    "project_name": "C"
  },
  {
    "favorite": "1",
    "project_name": "B"
  },
  {
    "favorite": "1",
    "project_name": "A"
  },

  {
    "favorite": "2",
    "project_name": "E"
  },
  {
    "favorite": "6",
    "project_name": "F"
  },
  {
    "favorite": "5",
    "project_name": "G"
  },
  {
    "favorite": "4",
    "project_name": "I"
  },
  {
    "favorite": "4",
    "project_name": "H"
  }
]

Template:

{{/* Build slice of unique, sorted favorites. */}}
{{- $favorites := slice -}}
{{- range .Site.Data.portfolio_residential -}}
  {{- $favorites = $favorites | append .favorite -}}
{{- end -}}
{{- $favorites = $favorites | uniq | sort -}}

{{/* Range over the sorted favorites, then range over a sorted subset of the data file. */}}
{{- range $favorites -}}
  {{- range where (sort $.Site.Data.portfolio_residential "project_name" "asc") "favorite" . -}}
    favorite = {{ .favorite }}<br>
    project_name = {{ .project_name }}<br><br>
  {{- end -}}
{{- end -}}
3 Likes

WOW! Great decision! Thanks for the time you spent! This really works even with separate data files

Credit to @FelicianoTech.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.