From 3eb178421d8f7261bf816cd2eb0910c0bfdaf83c Mon Sep 17 00:00:00 2001 From: loshiqi <553578653@qq.com> Date: Fri, 28 Oct 2022 11:32:59 +0800 Subject: [PATCH] =?UTF-8?q?map=E5=88=87=E7=89=87=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E9=9C=80=E5=A2=9E=E5=8A=A0=E6=8E=92=E5=BA=8F=E5=80=BC=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 48 ++++++++++++++++++++++++++++++++++-------------- map_test.go | 8 ++++---- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/map.go b/map.go index 8165b79..37d0bb2 100644 --- a/map.go +++ b/map.go @@ -205,19 +205,23 @@ func HttpBuildStringQuery(data map[string]string) string { /** * 对map切片 按值进行顺序 - * @param order desc/asc MapDate []map[string]interface{} + * @param order desc/asc val_type float64/string MapDate []map[string]interface{} * @2022/10/27 * @lo */ -func MapSort(order, val string, MapDate []map[string]interface{}) ([]map[string]interface{}, error) { +func MapSort(order, val, val_type string, MapDate []map[string]interface{}) ([]map[string]interface{}, error) { type KVPair struct { - Key int - Val string + Key int + Val_float64 float64 + val_string string } res := []map[string]interface{}{} if order != "desc" && order != "asc" { return res, errors.New("order parameter err") } + if val_type != "float64" && val_type != "string" { + return res, errors.New("val_type parameter err") + } if val == "" { return res, errors.New("val parameter err") } @@ -225,17 +229,33 @@ func MapSort(order, val string, MapDate []map[string]interface{}) ([]map[string] return res, errors.New("MapDate parameter err") } tmpList := []KVPair{} - for k, item := range MapDate { - tmpList = append(tmpList, KVPair{Key: k, Val: ToStr(item[val])}) - } - if order == "desc" { - sort.Slice(tmpList, func(i, j int) bool { - return tmpList[i].Val > tmpList[j].Val // 降序 - }) + if val_type == "string" { + for k, item := range MapDate { + tmpList = append(tmpList, KVPair{Key: k, val_string: ToStr(item[val])}) + } + if order == "desc" { + sort.Slice(tmpList, func(i, j int) bool { + return tmpList[i].val_string > tmpList[j].val_string // 降序 + }) + } else { + sort.Slice(tmpList, func(i, j int) bool { + return tmpList[i].val_string < tmpList[j].val_string // 升序 + }) + } } else { - sort.Slice(tmpList, func(i, j int) bool { - return tmpList[i].Val < tmpList[j].Val // 升序 - }) + for k, item := range MapDate { + float64_val, _ := ToFloat64(item[val]) + tmpList = append(tmpList, KVPair{Key: k, Val_float64: float64_val}) + } + if order == "desc" { + sort.Slice(tmpList, func(i, j int) bool { + return tmpList[i].Val_float64 > tmpList[j].Val_float64 // 降序 + }) + } else { + sort.Slice(tmpList, func(i, j int) bool { + return tmpList[i].Val_float64 < tmpList[j].Val_float64 // 升序 + }) + } } for _, tmp := range tmpList { res = append(res, MapDate[tmp.Key]) diff --git a/map_test.go b/map_test.go index 4c1954a..c2dfbad 100644 --- a/map_test.go +++ b/map_test.go @@ -6,15 +6,15 @@ import ( func Test_HttpBuildQuery(t *testing.T) { - a := map[string]interface{}{"id": "1", "name": "8","price":"2.3"} - b := map[string]interface{}{"id": "2", "name": "7","price":"2.8"} - c := map[string]interface{}{"id": "3", "name": "9","price":"2.1"} + a := map[string]interface{}{"id": "11", "name": "8", "price": "2.3"} + b := map[string]interface{}{"id": "4", "name": "7", "price": "2.8"} + c := map[string]interface{}{"id": "3", "name": "9", "price": "2.1"} list := []map[string]interface{}{} list = append(list, a) list = append(list, b) list = append(list, c) //ret := HttpBuildQuery(list) - ret, err := MapSort("asc", "name", list) + ret, err := MapSort("desc", "id", "float64", list) if err != nil {