package main import ( "fmt" "sort" ) func main() { //待排序队列 var stuScore = map[string]int{"ee":20,"cc":90,"ff":70,"qq":40,"aa":79,"bb":30} //创建切片,遍历map的值 var nums = []int{} for _,v:=range stuScore { nums=append(nums,v) } //对切片按值排序 //sort.Ints(nums) sort.Sort(sort.Reverse(sort.IntSlice(nums))) fmt.Println(nums) for _,num:=range nums { //遍历map的key和值,与切片中的元素做比对 for k,v:=range stuScore { if v==num { fmt.Println(k,v) } } } }
标签:sort,map,nums,fmt,range,go,排序 From: https://www.cnblogs.com/zhutao2014/p/16724980.html