golang中对map操作类

                                                                                                     
 
 
  1. package beeku  

  2.   

  3. import (  

  4.     "sort"  

  5. )  

  6.   

  7. type MapSorter struct {  

  8.     Keys []string  

  9.     Vals []string  

  10. }  

  11.   

  12. func NewMapSorter(m map[string]string) *MapSorter {  

  13.     ms := &MapSorter{  

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

  15.         Vals: make([]string, 0, len(m)),  

  16.     }  

  17.     for k, v := range m {  

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

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

  20.     }  

  21.     return ms  

  22. }  

  23.   

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

  25.     sort.Sort(ms)  

  26. }  

  27.   

  28. func (ms *MapSorter) Len() int           { return len(ms.Keys) }  

  29. func (ms *MapSorter) Less(i, j int) bool { return ms.Keys[i] < ms.Keys[j] }  

  30. func (ms *MapSorter) Swap(i, j int) {  

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

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

  33. }  





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值