|
@@ -0,0 +1,122 @@
|
|
|
+// sort
|
|
|
+package sort
|
|
|
+
|
|
|
+import (
|
|
|
+ "sort"
|
|
|
+
|
|
|
+ . "app.yhyue.com/moapp/jybase/common"
|
|
|
+)
|
|
|
+
|
|
|
+type SortObject struct {
|
|
|
+ Key string
|
|
|
+ Value int
|
|
|
+ Values []interface{}
|
|
|
+}
|
|
|
+
|
|
|
+type SortStruct []*SortObject
|
|
|
+
|
|
|
+func (list SortStruct) Len() int {
|
|
|
+ return len(list)
|
|
|
+}
|
|
|
+
|
|
|
+func (list SortStruct) Less(i, j int) bool {
|
|
|
+ if list[i].Value > list[j].Value {
|
|
|
+ return true
|
|
|
+ } else if list[i].Value < list[j].Value {
|
|
|
+ return false
|
|
|
+ } else if list[i].Value == list[j].Value {
|
|
|
+ if len(list[i].Values) > 0 {
|
|
|
+ t1 := IntAll(list[i].Values[0])
|
|
|
+ t2 := IntAll(list[j].Values[0])
|
|
|
+ if t1 > t2 {
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return list[i].Key < list[j].Key
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (list SortStruct) Swap(i, j int) {
|
|
|
+ var temp *SortObject = list[i]
|
|
|
+ list[i] = list[j]
|
|
|
+ list[j] = temp
|
|
|
+}
|
|
|
+
|
|
|
+func SortMap(list []*SortObject) []*SortObject {
|
|
|
+ ls := SortStruct(list)
|
|
|
+ sort.Sort(ls)
|
|
|
+ return ls
|
|
|
+}
|
|
|
+
|
|
|
+//通用排序
|
|
|
+type ComSortKey struct {
|
|
|
+ Keys []string //排序的key
|
|
|
+ Order int //1:正排 -1:倒排
|
|
|
+ Type string //排序的key的数据类型:string float int
|
|
|
+}
|
|
|
+type ComSortList struct {
|
|
|
+ SortKeys []*ComSortKey
|
|
|
+ List []*map[string]interface{}
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ComSortList) Len() int {
|
|
|
+ return len(s.List)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ComSortList) Less(i, j int) bool {
|
|
|
+ for _, v := range s.SortKeys {
|
|
|
+ var i_v interface{}
|
|
|
+ var j_v interface{}
|
|
|
+ for _, key := range v.Keys {
|
|
|
+ if i_v == nil {
|
|
|
+ i_v = (*s.List[i])[key]
|
|
|
+ }
|
|
|
+ if j_v == nil {
|
|
|
+ j_v = (*s.List[j])[key]
|
|
|
+ }
|
|
|
+ if i_v != nil && j_v != nil {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if i_v == j_v {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if v.Type == "string" {
|
|
|
+ return ObjToString(i_v) > ObjToString(j_v) && v.Order < 0
|
|
|
+ } else if v.Type == "float" {
|
|
|
+ return Float64All(i_v) > Float64All(j_v) && v.Order < 0
|
|
|
+ } else {
|
|
|
+ return Int64All(i_v) > Int64All(j_v) && v.Order < 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ComSortList) Swap(i, j int) {
|
|
|
+ s.List[i], s.List[j] = s.List[j], s.List[i]
|
|
|
+}
|
|
|
+
|
|
|
+//合并数组
|
|
|
+func (s *ComSortList) Merge(key string, o interface{}, maxLen int) *ComSortList {
|
|
|
+ repeat := map[string]bool{}
|
|
|
+ for _, v := range s.List {
|
|
|
+ repeat[ObjToString((*v)[key])] = true
|
|
|
+ }
|
|
|
+ os, _ := o.([]interface{})
|
|
|
+ for _, v := range os {
|
|
|
+ vm, _ := v.(map[string]interface{})
|
|
|
+ if repeat[ObjToString(vm[key])] {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ s.List = append(s.List, &vm)
|
|
|
+ }
|
|
|
+ sort.Sort(s)
|
|
|
+ if maxLen > 0 && len(s.List) > maxLen {
|
|
|
+ s.List = s.List[:maxLen]
|
|
|
+ }
|
|
|
+ return s
|
|
|
+}
|