/
sort.go
76 lines (65 loc) · 1.59 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
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
70
71
72
73
74
75
76
package values
import (
"reflect"
"sort"
)
// Sort any []interface{} value.
func Sort(data []interface{}) {
sort.Sort(genericSortable(data))
}
type genericSortable []interface{}
// Len is part of sort.Interface.
func (s genericSortable) Len() int {
return len(s)
}
// Swap is part of sort.Interface.
func (s genericSortable) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less is part of sort.Interface.
func (s genericSortable) Less(i, j int) bool {
return Less(s[i], s[j])
}
// SortByProperty sorts maps on their key indices.
func SortByProperty(data []interface{}, key string, nilFirst bool) {
sort.Sort(sortableByProperty{data, key, nilFirst})
}
type sortableByProperty struct {
data []interface{}
key string
nilFirst bool
}
// Len is part of sort.Interface.
func (s sortableByProperty) Len() int {
return len(s.data)
}
// Swap is part of sort.Interface.
func (s sortableByProperty) Swap(i, j int) {
data := s.data
data[i], data[j] = data[j], data[i]
}
// Less is part of sort.Interface.
func (s sortableByProperty) Less(i, j int) bool {
// index returns the value at s.key, if in is a map that contains this key
index := func(i int) interface{} {
value := ToLiquid(s.data[i])
rt := reflect.ValueOf(value)
if rt.Kind() == reflect.Map && rt.Type().Key().Kind() == reflect.String {
elem := rt.MapIndex(reflect.ValueOf(s.key))
if elem.IsValid() {
return elem.Interface()
}
}
return nil
}
a, b := index(i), index(j)
switch {
case a == nil && b == nil:
return false
case a == nil:
return s.nilFirst
case b == nil:
return !s.nilFirst
}
return Less(a, b)
}