-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort.go
49 lines (40 loc) · 960 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
package template
import (
"reflect"
"sort"
"github.com/CloudyKit/jet/v6"
"go.mongodb.org/mongo-driver/bson"
)
//排序 start
type MapsSort struct {
Key string
MapList []bson.M
Desc bool
}
func (m *MapsSort) Len() int {
return len(m.MapList)
}
func (m *MapsSort) Less(i, j int) bool {
if m.Desc {
return m.MapList[i][m.Key].(float64) > m.MapList[j][m.Key].(float64)
} else {
return m.MapList[i][m.Key].(float64) < m.MapList[j][m.Key].(float64)
}
}
func (m *MapsSort) Swap(i, j int) {
m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
}
func Sort(key string, maps []bson.M, desc bool) []bson.M {
mapsSort := MapsSort{}
mapsSort.Key = key
mapsSort.MapList = maps
mapsSort.Desc = desc
sort.Sort(&mapsSort)
return mapsSort.MapList
}
func sortFunc() jet.Func {
return func(a jet.Arguments) reflect.Value {
m := a.Get(0).Interface().([]bson.M)
return reflect.ValueOf(Sort(a.Get(1).String(), m, a.Get(2).Bool()))
}
}