-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsort.go
48 lines (38 loc) · 987 Bytes
/
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
44
45
46
47
48
package utils
import (
"sort"
zap "github.com/Laisky/zap"
"github.com/Laisky/go-utils/v5/log"
)
// SortBiggest sort from biggest to smallest
func SortBiggest(items PairList) PairList {
sort.Sort(sort.Reverse(items))
return items
}
// SortSmallest sort from smallest to biggest
func SortSmallest(items PairList) PairList {
sort.Sort(items)
return items
}
// SortItemItf interface of sort item
type SortItemItf interface {
GetValue() int
GetData() any
}
// PairList array of sort items
type PairList []SortItemItf
// Len return length of sort items
func (p PairList) Len() int {
log.Shared.Debug("len", zap.Int("len", len(p)))
return len(p)
}
// Less compare two items
func (p PairList) Less(i, j int) bool {
log.Shared.Debug("less compare", zap.Int("i", i), zap.Int("j", j))
return p[i].GetValue() < p[j].GetValue()
}
// Swap change two items
func (p PairList) Swap(i, j int) {
log.Shared.Debug("swap", zap.Int("i", i), zap.Int("j", j))
p[i], p[j] = p[j], p[i]
}