golink: truncate clicks slice after sorting

previously, the 200 links included in the "popular links" list were
non-deterministic (though those 200 were sorted). Now, always shows the
top 200.
This commit is contained in:
Will Norris
2022-11-09 13:18:01 -08:00
committed by Will Norris
parent 8e1aaaf768
commit 555d1339a8
+3 -3
View File
@@ -221,15 +221,15 @@ func serveHome(w http.ResponseWriter, short string) {
}
stats.mu.Unlock()
if len(clicks) > 200 {
clicks = clicks[:200]
}
sort.Slice(clicks, func(i, j int) bool {
if clicks[i].NumClicks != clicks[j].NumClicks {
return clicks[i].NumClicks > clicks[j].NumClicks
}
return clicks[i].Short < clicks[j].Short
})
if len(clicks) > 200 {
clicks = clicks[:200]
}
homeTmpl.Execute(w, homeData{
Short: short,