-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsort.go
43 lines (36 loc) · 1.26 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package trueskill
import "sort"
type playersByRank struct {
data []Player
rank []int
}
func (t playersByRank) Len() int { return len(t.data) }
func (t playersByRank) Swap(i, j int) {
t.data[i], t.data[j] = t.data[j], t.data[i]
t.rank[i], t.rank[j] = t.rank[j], t.rank[i]
}
func (t playersByRank) Less(i, j int) bool { return t.rank[i] < t.rank[j] }
// SortPlayersByRank sorts players by their rankings. The returned values
// have the same length as the provided arguments but are sorted from
// lowest (best) to highest (worst) rank.
func SortPlayersByRank(data []Player, rank []int) ([]Player, []int) {
sort.Sort(playersByRank{data, rank})
return data, rank
}
type teamsByRank struct {
data []Team
rank []int
}
func (t teamsByRank) Len() int { return len(t.data) }
func (t teamsByRank) Swap(i, j int) {
t.data[i], t.data[j] = t.data[j], t.data[i]
t.rank[i], t.rank[j] = t.rank[j], t.rank[i]
}
func (t teamsByRank) Less(i, j int) bool { return t.rank[i] < t.rank[j] }
// SortTeamsByRank sorts teams by their rankings. The returned values
// have the same length as the provided arguments but are sorted from
// lowest (best) to highest (worst) rank.
func SortTeamsByRank(data []Team, rank []int) ([]Team, []int) {
sort.Sort(teamsByRank{data, rank})
return data, rank
}