编辑代码

package main
import (
    "fmt"
    "sort"
    "encoding/json"
)

// ================== 按照 map  value 排序 ==================

type Pair struct {
    Key   string
    Value int64
}

type PairList []Pair
func (p PairList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p PairList) Len() int           { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value >= p[j].Value }

func main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
    
    TagSortMap := map[string]int64{
        "trade_group_buy":                             22900,
        "pre_sale_order":                              22800,
        "sku_promise_ship_rule_tag":                   22800,
        "sku_promise_receipt_rule_tag":                22800,
        "virtual_goods_tag":                           22700,
        "delivery_time_tag":                           22600,
        "support_7days_refund":                        22000,
        "unsupport_7days_refund_after_opening":        22000,
        "unsupport_7days_refund":                      22000,
        "support_15days_refund":                       22000,
        "unsupport_15days_refund_after_opening":       22000,
        "unsupport_15days_refund":                     22000,
        "unsupport_7days_refund_after_install":        22000,
        "unsupport_7days_refund_after_active":         22000,
        "unsupport_7days_refund_after_use":            22000,
        "unsupport_7days_refund_customize":            22000,
        "unsupport_7days_refund_public_and_contract":  22000,
        "unsupport_7days_refund_contract":             22000,
        "unsupport_15days_refund_after_install":       22000,
        "unsupport_15days_refund_after_active":        22000,
        "unsupport_15days_refund_after_use":           22000,
        "unsupport_15days_refund_customize":           22000,
        "unsupport_15days_refund_public_and_contract": 22000,
        "unsupport_15days_refund_contract":            22000,
        "breakage_refund":                             21900,
        "allergy_refund":                              21900,
        "diaper_rash_refund":                          21900,
        "pay_for_bad":                                 21900,
        "pfc_price_protection":                        21800,
        "insurance_real_onlive":                       21000,
        "buy_at_ease":                                 11000,
        "quality_authentication_btas":                 10000,
        "pay_for_dead_insurance_2":                    8901,
        "pay_for_dead_insurance_1":                    8900,
        "before_class_done_refund":                    8900,
        "before_24_class_start_refund":                8800,
        "seven_day_not_study_refund":                  8700,
        "not_support_refund":                          8600,
        "freight_insurance":                           6900,
        "allergy_insurance":                           5900,
        "any_time_refund":                             5800,
        "expired_auto_refund":                         5700,
        "refund_speed":                                5600,
        "delay_send_pay_money":                        3900,
        "ondoor_pickup":                               2900,
        "cert_insurance":                              2800,
    }


    pList := make(PairList, 0, len(TagSortMap))
    
    //  map 转换成 slice
    for k, v := range TagSortMap {
        pList = append(pList, Pair{Key: k, Value: v})
    }

    // 排序 slice
    sort.Sort(pList)
    fmt.Println(pList)

    str, _ := json.Marshal(pList)
    fmt.Println(str)

}