Sorting a Collection by a function/derived-from of a Params value

I want to sort a collection ($paginator.Pages in my case) by a value derived by aggregating a Param: score for all Pages with that Title. So

  1. Created a partial function that returns the aggregated value across all pages with the same Title
  2. I then use Store.Set to attach the aggregated value returned by the partial to each Page like this
{{ range $paginator.Pages }}
    {{ .Store.Set "totalScore" (partial "score-aggregator.html" .LinkTitle) }}
{{ end }}
  1. Finally I use the following expression
{{ range sort $paginator.Pages (.Store.Get "totalScore") "desc" }}
    {{/* do something with each page */}}
{{ end }}

While I get no errors, the sort order is clearly incorrect. Can someone help with what I am doing wrong? I have confirmed that the partial returns the correct value.

Thanks and regards

For this one the context is the current page, so that won’t work.

i could think of to using a map in your first loop and store all pages with score as key
score = page (or pages if the score is not uniq)
then sort the map by score which will remove the key and leave the page(s) for each score.

Thanks!! That is a very promising solution and one that I plan to implement

But in the interest of learning, I am intrigued by

Which context might you be refrring to? Inside the range? or the context passed to the Page? I am using this to show posts on the paginated Home page

the Bold one refers to the current page where the range is executed, so your homepage
inside the range it would refer to the paginated page

there’s no context switch within the {{ range ... }action

HERE DOT is current page (HOMEPAGE)
{{ range sort $paginator.Pages (.Store.Get “totalScore”) “desc” }}
  here DOT is a paginated page
{{ end }}

Ahhh! Understood.

Thanks!

also that one might hit you when playing around with other pages store.

Yes that is an issue (bit me earlier in another project) - In this case I am using Store in “main” so that is thankfully moot.

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