golang中对map操作类

前端之家收集整理的这篇文章主要介绍了golang中对map操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
                                                                                                     
 
 
  1. packagebeeku

  2. import(

  3. "sort"

  4. )

  5. typeMapSorterstruct{

  6. Keys[]string

  7. Vals[]string

  8. }

  9. funcNewMapSorter(mmap[string]string)*MapSorter{

  10. ms:=&MapSorter{

  11. Keys:make([]string,len(m)),

  12. Vals:make([]string,0);">fork,v:=rangem{

  13. ms.Keys=append(ms.Keys,k)

  14. ms.Vals=append(ms.Vals,v)

  15. returnms

  16. func(ms*MapSorter)Sort(){

  17. sort.Sort(ms)

  18. func(ms*MapSorter)Len()int{returnlen(ms.Keys)}

  19. func(ms*MapSorter)Less(i,jint)bool{returnms.Keys[i]<ms.Keys[j]}

  20. func(ms*MapSorter)Swap(i,jint){

  21. ms.Vals[i],ms.Vals[j]=ms.Vals[j],ms.Vals[i]

  22. ms.Keys[i],ms.Keys[j]=ms.Keys[j],ms.Keys[i]

  23. }





查看原文:http://www.zoues.com/2016/10/27/golang%e4%b8%ad%e5%af%b9map%e6%93%8d%e4%bd%9c%e7%b1%bb/

猜你在找的Go相关文章