当前位置: 首页 > news >正文

电子商务网站建设价格定制网站开发与模板

电子商务网站建设价格,定制网站开发与模板,做网站用哪个服务器,简单的网站源码原文链接#xff1a; Go 语言中排序的 3 种方法 在写代码过程中#xff0c;排序是经常会遇到的需求#xff0c;本文会介绍三种常用的方法。 废话不多说#xff0c;下面正文开始。 使用标准库 根据场景直接使用标准库中的方法#xff0c;比如#xff1a; sort.Intsso…原文链接 Go 语言中排序的 3 种方法 在写代码过程中排序是经常会遇到的需求本文会介绍三种常用的方法。 废话不多说下面正文开始。 使用标准库 根据场景直接使用标准库中的方法比如 sort.Intssort.Float64ssort.Strings 举个例子 s : []int{4, 2, 3, 1} sort.Ints(s) fmt.Println(s) // [1 2 3 4]自定义比较器 使用 sort.Slice 方法排序时可以自定义比较函数 less(i, j int) bool这样就可以根据需要按不同的字段进行排序。 如果想要稳定排序的话就使用 sort.SliceStable 方法。 举个例子 family : []struct {Name stringAge int }{{Alice, 23},{David, 2},{Eve, 2},{Bob, 25}, }// Sort by age, keeping original order or equal elements. sort.SliceStable(family, func(i, j int) bool {return family[i].Age family[j].Age }) fmt.Println(family) // [{David 2} {Eve 2} {Alice 23} {Bob 25}]自定义数据结构 使用 sort.Sort 或者 sort.Stable 方法它们可以对任意实现了 sort.Interface 的数据结构排序。 type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with// index i should sort before the element with index j.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int) }意思就是说只要某一个数据结构实现了 Len() intLess(i, j int) bool 和 Swap(i, j int) 这三个方法那么就可以使用 sort.Sort 来排序。 举个例子 type Person struct {Name stringAge int }// ByAge implements sort.Interface based on the Age field. type ByAge []Personfunc (a ByAge) Len() int { return len(a) } func (a ByAge) Less(i, j int) bool { return a[i].Age a[j].Age } func (a ByAge) Swap(i, j int) { a[i], a[j] a[j], a[i] }func main() {family : []Person{{Alice, 23},{Eve, 2},{Bob, 25},}sort.Sort(ByAge(family))fmt.Println(family) // [{Eve 2} {Alice 23} {Bob 25}] }字典排序 我们都知道字典是无序的具体原因可以看之前写的这篇文章 Go 语言 map 如何顺序读取 如果想要字典按 key 或者 value 排序的话可以这样做。 m : map[string]int{Alice: 2, Cecil: 1, Bob: 3}keys : make([]string, 0, len(m)) for k : range m {keys append(keys, k) } sort.Strings(keys)for _, k : range keys {fmt.Println(k, m[k]) } // Output: // Alice 2 // Bob 3 // Cecil 1以上就是本文的全部内容如果觉得还不错的话欢迎点赞转发和关注感谢支持。 参考文章 https://yourbasic.org/golang/how-to-sort-in-go/#performance-and-implementation 推荐阅读 Go 语言 map 是并发安全的吗Go 语言切片是如何扩容的Go 语言数组和切片的区别Go 语言 new 和 make 关键字的区别为什么 Go 不支持 []T 转换为 []interface为什么 Go 语言 struct 要使用 tags
http://www.zqtcl.cn/news/628453/

相关文章:

  • 通过dede访问自己做的网站高端 建站
  • wordpress自定义json温岭新站seo
  • 网站开发的五个阶段wordpress安装在本地
  • 郴州网站建设有哪些sem优化
  • 在百度怎么申请自己的网站深圳网站建设迅美
  • wordpress 企业网站教程网站开发集成软件
  • 专业的西安免费做网站wordpress手机端插件
  • 口碑好网站建设优化大师win10下载
  • 网站建设普及型小程序开发平台好的有哪些
  • 网站建设与管理专业凡科做的网站好吗
  • wordpress添加变量福州seo网站管理
  • 哔哩哔哩免费网站观看网站制作合同书
  • 自流井移动网站建设建设网站的一般步骤
  • 手机导航网站模板上海低价网站建设
  • 如何开公司注册需要多少钱东莞网站推广优化网上推广公司
  • 新闻门户网站制作教育培训网站开发
  • 网站建设公司哪个好一点最近一周的热点新闻
  • 做最优秀的自己的视频网站佛山搜索引擎优化
  • 六盘水市网站建设免费封面设计在线制作生成
  • 北京快速建站制作公司wordpress wpoptions
  • iis如何建立网站门源县住房和城乡建设局网站
  • 装修素材图片都从什么网站找铁门关网站建设
  • 网站服务器环境不支持mysql数据库免费商标图案logo
  • 以什么主题做网站好wordpress怎么设置404
  • 为什么手机进网站乱码网络营销工具的特点
  • DW怎么做网站下拉菜单网站建设外包网站
  • 手机做兼职的网站设计公司注册记账代理公司
  • 如何在vs做网站建筑工程电影网
  • 甘肃网站开发网站建设自己在家接单
  • 龙岗网站制作资讯福田区龙岗区发布通告