package main
import (
"sort"
"fmt"
)
func main(){
user := make(map[string]string)
user["name"] = "小红"
user["age"] = "18"
user["sex"] = "女"
var keys []string
for k := range user {
keys = append(keys, k)
}
//按字典升序排列
sort.Strings(keys)
//按字典降序排列
//sort.Sort(sort.Reverse(sort.StringSlice(keys)))
for _, k := range keys {
fmt.Println("Key:", k, "Value:", user[k])
}
}
输出
Key: age Value: 18
Key: name Value: 小红
Key: sex Value: 女
func sortByColumn(column string, data []RespUserShowAgentInfo) {
sort.Slice(data, func(i, j int) bool {
switch column {
case "appname":
return data[i].Appname < data[j].Appname
default:
return data[i].Appname < data[j].Appname
}
})
}