forked from osteele/liquid
/
sort_filters.go
71 lines (63 loc) · 1.47 KB
/
sort_filters.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
package filters
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/osteele/liquid/values"
)
func sortFilter(array []interface{}, key interface{}) []interface{} {
result := make([]interface{}, len(array))
copy(result, array)
if key == nil {
values.Sort(result)
} else {
values.SortByProperty(result, fmt.Sprint(key), true)
}
return result
}
func sortNaturalFilter(array []interface{}, key interface{}) interface{} {
result := make([]interface{}, len(array))
copy(result, array)
switch {
case reflect.ValueOf(array).Len() == 0:
case key != nil:
sort.Sort(keySortable{result, func(m interface{}) string {
rv := reflect.ValueOf(m)
if rv.Kind() != reflect.Map {
return ""
}
ev := rv.MapIndex(reflect.ValueOf(key))
if ev.CanInterface() {
if s, ok := ev.Interface().(string); ok {
return strings.ToLower(s)
}
}
return ""
}})
case reflect.TypeOf(array[0]).Kind() == reflect.String:
sort.Sort(keySortable{result, func(s interface{}) string {
return strings.ToUpper(s.(string))
}})
}
return result
}
type keySortable struct {
slice []interface{}
keyFn func(interface{}) string
}
// Len is part of sort.Interface.
func (s keySortable) Len() int {
return len(s.slice)
}
// Swap is part of sort.Interface.
func (s keySortable) Swap(i, j int) {
a := s.slice
a[i], a[j] = a[j], a[i]
}
// Less is part of sort.Interface.
func (s keySortable) Less(i, j int) bool {
k, sl := s.keyFn, s.slice
a, b := k(sl[i]), k(sl[j])
return a < b
}