golang []map[string][string] , 先按大小,再按键名排序



type IpSortItem struct {
	Value int    `json:"value"`
	Name  string `json:"name"`
}
// 
先按数量排序,再按名称排序
func sortIPView(rspData []map[string]string) []*IpSortItem {

	var totalList []int
	dataList := make(map[int][]string)

	for _, value := range rspData {
		for key2, value2 := range value {
			num, err := strconv.Atoi(value2)
			if err != nil {
				logUtil.Errorf("返回的格式错误%s:%s", key2, value2)
			}
			_, ok := dataList[num]
			if ok == false {
				dataList[num] = []string{}
			}
			dataList[num] = append(dataList[num], key2)

			if MatchInArrayInt(num, totalList) == false {
				totalList = append(totalList, num)
			}
		}
	}

	sort.Sort(sort.Reverse(sort.IntSlice(totalList)))
	var data []*IpSortItem
	for _, value := range totalList {
		value2, ok := dataList[value]
		if ok == false {
			continue
		}
		sort.Strings(value2)
		for _, value3 := range value2 {
			item := new(IpSortItem)
			item.Name = value3
			item.Value = value
			data = append(data, item)
		}
	}
	return data
}

// MatchInArray returns true if the given string value is in the array.
func MatchInArrayInt(value int, arr []int) bool {
	for _, v := range arr {
		if v == value {
			return true
		}
	}
	return false
}
上一篇:vs2017编写模板类出现以下错误时:没有与参数列表匹配的构造函数……


下一篇:AcWing 232. 守卫者的挑战 (期望DP)打卡