-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.go
69 lines (56 loc) · 1014 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ezdb
type sortable[T any] struct {
Key string
Value T
}
type keySort struct {
a []string
f SortFunc[string]
}
func (s *keySort) Len() int {
return len(s.a)
}
func (s *keySort) Less(i, j int) bool {
return s.f(s.a[i], s.a[j])
}
func (s *keySort) Result() []string {
return s.a
}
func (s *keySort) Swap(i, j int) {
a := s.a[i]
b := s.a[j]
s.a[i] = b
s.a[j] = a
}
type valueSort[T any] struct {
a []*sortable[T]
f SortFunc[T]
}
func (s *valueSort[T]) Len() int {
return len(s.a)
}
func (s *valueSort[T]) Less(i, j int) bool {
a := s.a[i]
b := s.a[j]
return s.f(a.Value, b.Value)
}
func (s *valueSort[T]) Result() []string {
k := []string{}
for _, el := range s.a {
k = append(k, el.Key)
}
return k
}
func (s *valueSort[T]) Swap(i, j int) {
a := s.a[i]
b := s.a[j]
s.a[i] = b
s.a[j] = a
}
func makeSortable[T any](m map[string]T) []*sortable[T] {
a := []*sortable[T]{}
for key, value := range m {
a = append(a, &sortable[T]{Key: key, Value: value})
}
return a
}