m := map[string]int{"Alice": 23, "Eve": 2, "Bob": 25}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k, m[k])
}
m := map[string]int{"Alice": 23, "Eve": 2, "Bob": 25}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k, m[k])
}
本文标题:golang Map key排序
本文链接:https://www.haomeiwen.com/subject/sfexfktx.html
网友评论