Skip to content

Commit 555d133

Browse files
committed
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.
1 parent 8e1aaaf commit 555d133

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

golink.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ func serveHome(w http.ResponseWriter, short string) {
221221
}
222222
stats.mu.Unlock()
223223

224-
if len(clicks) > 200 {
225-
clicks = clicks[:200]
226-
}
227224
sort.Slice(clicks, func(i, j int) bool {
228225
if clicks[i].NumClicks != clicks[j].NumClicks {
229226
return clicks[i].NumClicks > clicks[j].NumClicks
230227
}
231228
return clicks[i].Short < clicks[j].Short
232229
})
230+
if len(clicks) > 200 {
231+
clicks = clicks[:200]
232+
}
233233

234234
homeTmpl.Execute(w, homeData{
235235
Short: short,

0 commit comments

Comments
 (0)