golang中按照结构体的某个字段排序实例代码
来源:脚本之家
2022-12-24 21:55:22
0浏览
收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《golang中按照结构体的某个字段排序实例代码》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
概述
golang的sort包默认支持int, float64, string的从小大到排序:
int -> Ints(x []int)
float64 -> Float64s(x []float64)
string -> Strings(x []string)
同时它还提供了自定义的排序接口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) }
golang默认提供了三个类型,他们都实现了Interface:
Float64Slice
IntSlice
StringSlice
从大到小排序
方法1:先使用提供的从大到小排序,再翻转
arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2} sort.Sort(sort.Reverse(sort.Float64Slice(arr))) fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]
方法二:自定义类型实现
type Float64SliceDecrement []float64 func (s Float64SliceDecrement) Len() int { return len(s) } func (s Float64SliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s Float64SliceDecrement) Less(i, j int) bool { return s[i] > s[j] } func main() { arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2} sort.Sort(Float64SliceDecrement(arr)) fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1] }
按照结构体的某个字段排序
按年纪从大到小排序
type Persons struct { Age int Height int } type PersonsSliceDecrement []Persons func (s PersonsSliceDecrement) Len() int { return len(s) } func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s PersonsSliceDecrement) Less(i, j int) bool { return s[i].Age > s[j].Age } func main() { arr1 := []Persons{ Persons{10, 12}, Persons{20, 12}, Persons{9, 12}, Persons{10, 12}, Persons{11, 12}, } sort.Sort(PersonsSliceDecrement(arr1)) fmt.Println(arr1) }
打印
[{20 12} {11 12} {10 12} {10 12} {9 12}]
按年纪从大到小,如果年纪相等的,按身高从小到到
type Persons struct { Age int Height int } type PersonsSliceDecrement []Persons func (s PersonsSliceDecrement) Len() int { return len(s) } func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s PersonsSliceDecrement) Less(i, j int) bool { if s[i].Age > s[j].Age { return true } if s[i].Age == s[j].Age && s[i].Height <p>打印</p> <blockquote><p>[{20 12} {10 11} {10 100} {10 110} {10 120}]</p></blockquote> <h2>使用 sort.Stable 进行稳定排序</h2> <p>sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable ,用法就是将sort.Sort 替换为 sort.Stable</p> <p>附:go根据结构体中任意字段进行排序</p> <h2>附:根据结构体中任意字段进行排序</h2> <p>Sort()</p> <p>Reverse()</p> <p>Less(i,j int) bool</p> <p>Len() int</p> <p>Swap(i,j int)</p> <pre class="brush:go;">package main import ( "fmt" "sort" ) type Student struct { Number string Name string Age int IsWalker bool Weight float32 } type ByNumber []*Student func (this ByNumber)Len() int { return len(this) } func (this ByNumber)Less(i,j int) bool { return this[i].Number<this func bynumber int this string const format="| %v |\t%v |\t%v |\t %v |\t %v |\t%v |\n" fmt.println for k fmt.printf return main1 sts:="[]*Student{" b:="ByNumber(sts)" sort.sort type customsort struct s less bool len this.s this.less main c:="&customSort{" s: sts less: j if i.number j.number>j.Number } if i.Name!=j.Name{ return i.Name<j.name return false package sort a type typically collection that satisfies sort.interface can be sorted by the routines in this package. methods require elements of enumerated an integer index. interface len is number collection. int less reports whether element with index i should before j. j bool swap swaps indexes and sort.sort fmt.println><h2>总结</h2> <p>今天关于《golang中按照结构体的某个字段排序实例代码》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!</p></j.name></this>
版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- Go error的使用方式详解

- 下一篇
- Golang异常处理之defer,panic,recover的使用详解
评论列表
-
- 幽默的狗
- 这篇文章内容出现的刚刚好,太全面了,受益颇多,已加入收藏夹了,关注博主了!希望博主能多写Golang相关的文章。
- 2023-01-04 12:17:35
查看更多
最新文章
-
- Golang · Go教程 | 23小时前 |
- TigervncDebian多用户共享桌面超简单教程
- 482浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Go语言新手必看!切片vs数组,一次搞定这两个核心知识点
- 472浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Docker在Debian上运行超简单教程(保姆级教学)
- 210浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Debian设置hostname踩坑记录:权限问题大揭秘
- 334浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Debian装SQLServer?这些问题你一定要注意!
- 284浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Debian系统下Jenkins自动化部署脚本教学
- 367浏览 收藏
-
- Golang · Go教程 | 2天前 |
- DebianSwap服务器应用实测,这些场景真的用得上!
- 319浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Debian跑TigerVNC实测!真香警告,快来看看性能咋样~
- 171浏览 收藏
-
- Golang · Go教程 | 2天前 |
- 在Debian上玩转SQLServer备份还原,手把手教你一步步操作
- 498浏览 收藏
-
- Golang · Go教程 | 2天前 |
- DebianOverlay不会玩?手把手教你轻松定制化安装
- 258浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Go语言实战:time.Ticker&time.After用法区别及避坑技巧
- 240浏览 收藏
-
- Golang · Go教程 | 2天前 |
- Debian系统如何快速定位&干掉那些讨厌的僵尸进程
- 317浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 21次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 50次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 58次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 53次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 60次使用
查看更多
相关文章
-
- mysql timestamp字段规范使用详情
- 2023-01-02 206浏览
-
- Go语言实现常用排序算法的示例代码
- 2022-12-31 178浏览
-
- Golang中Map按照Value大小排序的方法实例
- 2023-02-18 493浏览
-
- 详解go语言中sort如何排序
- 2023-01-07 189浏览
-
- golang编程开发使用sort排序示例详解
- 2023-02-25 459浏览