Skip to content

Commit

Permalink
sort results page slightly different
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasf committed Apr 26, 2024
1 parent 3bccaa8 commit 7aa8c0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ func (s *Server) Results() AppHandler {
} else {
battle.Entries.Shuffle()
}
rest := topPlaces.Diff(battle.Entries)
rest.SortByName()
templateData := struct {
Title string
Battle db.Battle
Expand All @@ -244,7 +246,7 @@ func (s *Server) Results() AppHandler {
SumScores: sumScores,
Config: s.ServerConfig,
TopPlaces: topPlaces,
Rest: topPlaces.Diff(battle.Entries),
Rest: rest,
}

w.WriteHeader(http.StatusOK)
Expand Down
15 changes: 13 additions & 2 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (e Entries) SortByID() {
})
}

func (e Entries) SortByName() {
slices.SortFunc(e, func(a, b Entry) int {
if v := cmp.Compare(a.Author, b.Author); v != 0 {
return v
}
if v := cmp.Compare(a.Title, b.Title); v != 0 {
return v
}
return cmp.Compare(a.ID, b.ID)
})
}

func (e Entries) Shuffle() {

rnd := rand.New(rand.NewChaCha8([32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}))
Expand Down Expand Up @@ -83,9 +95,8 @@ func (e Entries) Places(scoreMap ScoreMap) Places {
}

}
currentPlace.SortByID()
currentPlace.Shuffle()

currentPlace.SortByName()
topPlaces = append(topPlaces, currentPlace)

return topPlaces
Expand Down

0 comments on commit 7aa8c0b

Please sign in to comment.