注:新版本已经有序
package main
import (
"fmt"
"sort"
)
func main() {
//map排序
map1 := make(map[int]int, 10)
map1[10] = 100
map1[1] = 13
map1[4] = 56
map1[8] = 90
fmt.Println(map1)
//将mao的key放到切片中 对切片排序 遍历切片然后按照key来输出map的值
var keys []int
for k, _ := range map1 {
keys = append(keys, k)
}
//排序
sort.Ints(keys)
fmt.Println()
for _, k := range keys {
fmt.Printf("map[%v]=%v", k, map1[k])
}
}
输出结果: