-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsorts.go
36 lines (33 loc) · 857 Bytes
/
sorts.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
package main
import (
"github.com/oleiade/reflections"
"github.com/bradfitz/slice"
)
func sortProperties(field string, items Items) Items {
slice.Sort(items, func(i, j int) bool {
iValue, _ := reflections.GetField(items[i], field)
jValue, _ := reflections.GetField(items[j], field)
switch iValue.(type) {
case int:
return iValue.(int) > jValue.(int);
case float64:
return iValue.(float64) > jValue.(float64);
}
return false
})
return items
}
func reverseSortProperties(field string, items Items) Items {
slice.Sort(items, func(i, j int) bool {
iValue, _ := reflections.GetField(items[i], field)
jValue, _ := reflections.GetField(items[j], field)
switch iValue.(type) {
case int:
return iValue.(int) < jValue.(int);
case float64:
return iValue.(float64) < jValue.(float64);
}
return false
})
return items
}