Related Posts by Tag Count, then By Date

I’ve found a lot of support questions on sorting related posts, but none that incorporate a secondary sort. I’m trying to find the top 3 related posts by matching tag count with ties broken by date. I have a working example that sorts by date, but not sure how to store the tag count of each page and then sort by that count and then by date.

{{ range first 3 ( where ( where .Site.Pages ".Params.tags" "intersect" .Params.tags ) "Permalink" "!=" .Permalink ) }}
#### CONTENT HERE ####
{{ end }}

The code below seems like a step in the right direction, but again I’m a little lost on how to modify because I’m not super familiar with how to iterate through the variables effectively.

<div class="related-posts">
  <h5>Related Posts</h5>
  {{ $page_link := .Permalink }}
  {{ $tags := .Params.tags }}
  {{ $scratch := newScratch }}
  {{ range .Site.Pages.ByDate }}
    {{ $page := . }}
    {{ $tag_count := intersect $tags .Params.tags | len | printf "%05d" }}
    {{ $scratch.SetInMap "relatedposts" $tag_count  $page.Permalink }}
  {{ end }}
  {{ range first 3 ($scratch.GetSortedMapValues "relatedposts") }}
    {{ $related_page_link := . }}
    {{ if ne $page_link $related_page_link }}
      {{ range first 1 ( where .Site.Pages "Permalink" "==" $related_page_link) }}
            <ol class="post-list">
              <li class="post-stub">
                  <a href="{{ .Permalink }}">
                    <h5 class="post-stub-title">{{ .Title }}</h5>
                  </a>
                  <p class="post-stub-description">{{ .Date.Format "January 2, 2006" }}</p>
              </li>
            </ol>                    
      {{ end }}
    {{ end }}
  {{ end }}
</div>

Hi what is it you’re trying to achieve … sorry this didnt make sense reading it

I think you want 3 related posts - and this to be sorted in date order (from newest to oldest) … you may just need to check your includeNewer is set to true

Thanks for the quick reply @damien1 and sorry if it wasn’t clear. Hopefully the example below helps to illustrate where I have a current page and then 4 possible related pages to order on that current page and I want to list those pages in order of the number of matching tags to the current page with a secondary sort on the date (see at the bottom for the desired ordering).

Current Page

[[page]]
title = "Current page to list related posts on"
tags = ["A", "B", "C"]
date = "2020-07-14"

Other Pages
(listed chronologically, but primary sort should be # of tags matching the current page)

[[page]]
title = "Most recent, but only one tag match"
tags = ["B"]
date = "2020-07-13"

[[page]]
title = "Another recent one tag match, list it second of one tag matches"
tags = ["A"]
date = "2020-06-01"

[[page]]
title = "Three tag matches, but older (best tag match)"
tags = ["A", "B", "C"]
date = "2019-07-01"

[[page]]
title = "Two tag matches, but older"
tags = ["B", "C"]
date = "2017-09-01"

Desired List Order of Related Pages

Related Pages: 
 1. Three tag matches, but older (best tag match) - Jul 1, 2019
 2. Two tag matches, but older - Sep 1, 2017
 3. Most recent, but only one tag match - Jul 13, 2020
 4. Another recent one tag match, list it second of one tag matches - Jun 1, 2020

@damien1 Thanks so much for linking to the related documentation. It seems very robust in the way it can incorporate different weightings and indices. So while I haven’t got the logic exactly as described above, I found this to work well:

config.toml

[related]
  threshold = 80
  includeNewer = true
  toLower = true
  [[related.indices]]   
    name = "tags"
    weight = 100
  [[related.indices]]
    name = "date"
    weight = 10 

layouts/_default/single.html

 <div class="related-posts">
  <h5>Related Posts</h5>  
  {{ range first 3 ( where (.Site.RegularPages.Related . ) "Permalink" "!=" .Permalink ) }}
  <ol class="post-list">
    <li class="post-stub">
        <a href="{{ .Permalink }}">
          <h5 class="post-stub-title">{{ .Title }}</h5>
        </a>
        <p class="post-stub-description">{{ .Date.Format "January 2, 2006" }}</p>
    </li>
  </ol>
  {{ end }}
</div>