dxb350352的gravatar头像
dxb350352 2017-11-24 17:21:21

golang数组排序

官方只提供基本类型的排序,所以自己通过反射写了个能够排序struct的工具类

golang数组排序

func main() {
       var arr = []int{3, 2, 1, 4, 5}
       sort.Sort(arr, false)
       fmt.Println(arr)
       type Test struct {
              Id   int
              Name string
       }
       var arrt []Test
       arrt = append(arrt, Test{1, "A"})
       arrt = append(arrt, Test{1, "C"})
       arrt = append(arrt, Test{2, "A"})
       arrt = append(arrt, Test{2, "B"})
       arrt = append(arrt, Test{1, "B"})
       arrt = append(arrt, Test{2, "C"})
       sort.Sort(arrt, true, "Name", "Id")
       fmt.Println(arrt)
}

打赏

文件名:sort.rar,文件大小:1.062K 下载
  • /
    • /sort.go
最代码最近下载分享源代码列表最近下载
1458790210  LV2 2022年4月13日
SHELLI  LV1 2021年4月26日
laishaofe  LV7 2020年9月7日
最代码官方  LV167 2017年11月24日
最代码最近浏览分享源代码列表最近浏览
与哲rhcy  LV6 2023年5月10日
835512308  LV14 2022年11月23日
微信网友_6040315240812544  LV8 2022年10月31日
1458790210  LV2 2022年4月13日
靛紫1234  LV4 2021年12月17日
biubiuchen  LV11 2021年8月25日
趣多多不是脆脆鲨  LV2 2021年8月15日
_nrxsh  LV12 2021年7月8日
SHELLI  LV1 2021年4月26日
zhoumq  LV6 2021年1月27日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友