packagebeeku
import(
"sort"
)
typeMapSorterstruct{
Keys[]string
Vals[]string
}
funcNewMapSorter(mmap[string]string)*MapSorter{
ms:=&MapSorter{
Keys:make([]string,len(m)),
Vals:make([]string,0);">fork,v:=rangem{
ms.Keys=append(ms.Keys,k)
ms.Vals=append(ms.Vals,v)
returnms
func(ms*MapSorter)Sort(){
sort.Sort(ms)
func(ms*MapSorter)Len()int{returnlen(ms.Keys)}
func(ms*MapSorter)Less(i,jint)bool{returnms.Keys[i]<ms.Keys[j]}
func(ms*MapSorter)Swap(i,jint){
ms.Vals[i],ms.Vals[j]=ms.Vals[j],ms.Vals[i]
ms.Keys[i],ms.Keys[j]=ms.Keys[j],ms.Keys[i]
}
查看原文:http://www.zoues.com/2016/10/27/golang%e4%b8%ad%e5%af%b9map%e6%93%8d%e4%bd%9c%e7%b1%bb/