-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort.go
42 lines (34 loc) · 1.17 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
package collectjs
import (
"sort"
)
// A couple of type definitions to make the units clear.
type earthMass float64
type au float64
// By is the type of a "less" function that defines the ordering of its Planet arguments.
type By func(p1, p2 *Collect) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(collects []*Collect) {
ps := &collectSorter{
collects: collects,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ps)
}
// planetSorter joins a By function and a slice of Planets to be sorted.
type collectSorter struct {
collects []*Collect
by func(p1, p2 *Collect) bool // Closure used in the Less method.
}
// Len is part of sort.Interface.
func (s *collectSorter) Len() int {
return len(s.collects)
}
// Swap is part of sort.Interface.
func (s *collectSorter) Swap(i, j int) {
s.collects[i], s.collects[j] = s.collects[j], s.collects[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *collectSorter) Less(i, j int) bool {
return s.by(s.collects[i], s.collects[j])
}