-
Notifications
You must be signed in to change notification settings - Fork 6
/
sorter.go
40 lines (32 loc) · 1.23 KB
/
sorter.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
package sorter
import (
"sort"
"github.com/agencyenterprise/gossip-host/pkg/analysis/types"
)
// By is the type of a "less" function that defines the ordering of its Planet arguments.
type By func(m1, m2 *types.MessageLog) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(messageLogs []*types.MessageLog) {
ms := &MessageLogsSorter{
messageLogs: messageLogs,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ms)
}
// MessageLogSorter joins a By function and a slice of messageLogs to be sorted.
type MessageLogsSorter struct {
messageLogs []*types.MessageLog
by func(m1, m2 *types.MessageLog) bool // Closure used in the Less method.
}
// Len is part of sort.Interface.
func (s *MessageLogsSorter) Len() int {
return len(s.messageLogs)
}
// Swap is part of sort.Interface.
func (s *MessageLogsSorter) Swap(i, j int) {
s.messageLogs[i], s.messageLogs[j] = s.messageLogs[j], s.messageLogs[i]
}
// less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *MessageLogsSorter) Less(i, j int) bool {
return s.by(s.messageLogs[i], s.messageLogs[j])
}