map實(shí)現(xiàn)根據(jù)key值排列暴氏,必須先用切片取出key所有值祝蝠,排序后音诈,再從map中獲取值
一、代碼
绎狭、细溅、、
package main
import (
"fmt"
"sort"
)
//map實(shí)現(xiàn)根據(jù)key值排列坟岔,必須先用切片取出key所有值谒兄,排序后,再從map中獲取值
func sortMap(a map[int]int) {
var keys []int
for key, _ := range a {
keys = append(keys, key)
}
sort.Ints(keys)
for index, v := range keys {
fmt.Printf("a[%d] = %d\n", index, a[v])
}
}
func main() {
a := make(map[int]int)
a[3] = 1
a[1] = 2
a[6] = 3
a[4] = 9
a[8] = 34
sortMap(a)
}
社付、承疲、、
二鸥咖、圖片