golang编程开发使用sort排序示例分析
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容介绍了"golang编程开发使用sort排序示例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有
千家信息网最后更新 2025年11月07日golang编程开发使用sort排序示例分析
本篇内容介绍了"golang编程开发使用sort排序示例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
sort 操作的对象通常是一个 slice,需要满足三个基本的接口,并且能够使用整数来索引
// A type, typically a collection, that satisfies sort.Interface can be// sorted by the routines in this package. The methods require that the// elements of the collection be enumerated by an integer index. 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) }ex-1 对 []int 从小到大排序
package main import ( "fmt" "sort" ) type IntSlice []intfunc (s IntSlice) Len() int { return len(s) }func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] } func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }func main() { a := []int{4,3,2,1,5,9,8,7,6} sort.Sort(IntSlice(a)) fmt.Println("After sorted: ", a) }ex-2 使用 sort.Ints 和 sort.Strings
golang 对常见的 []int 和 []string 分别定义了 IntSlice 和 StringSlice, 实现了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接对 []int 和 []string 进行排序, 使用起来非常方便
package main import ("fmt" "sort" )func main() {a := []int{3, 5, 4, -1, 9, 11, -14} sort.Ints(a) fmt.Println(a)ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"} sort.Strings(ss)fmt.Println(ss) sort.Sort(sort.Reverse(sort.StringSlice(ss))) fmt.Printf("After reverse: %v\n", ss) }ex-3 使用 sort.Reverse 进行逆序排序
如果我们想对一个 sortable object 进行逆序排序,可以自定义一个type。但 sort.Reverse 帮你省掉了这些代码
package main import ( "fmt" "sort" )func main() { a := []int{4,3,2,1,5,9,8,7,6} sort.Sort(sort.Reverse(sort.IntSlice(a)))fmt.Println("After reversed: ", a) }ex-4 使用 sort.Stable 进行稳定排序
sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable
package main import ( "fmt" "sort" )type person struct {Name stringAge int}type personSlice []person func (s personSlice) Len() int { return len(s) }func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age } func main() {a := personSlice {{ Name: "AAA",Age: 55,}, {Name: "BBB",Age: 22,}, {Name: "CCC",Age: 0,},{Name: "DDD",Age: 22,},{ Name: "EEE",Age: 11, },}sort.Stable(a)fmt.Println(a) }"golang编程开发使用sort排序示例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
排序
示例
分析
开发
编程
内容
接口
更多
知识
逆序
j.
实用
从小到大
学有所成
接下来
三个
从小
代码
困境
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
局域网设置时间服务器
网络安全的安全控制包括哪三个
广东电上店下网络技术游戏
石龙软件开发
读多写少数据库推荐
服务器文件夹设置禁止删除
上海银行 软件开发
数据库是数字如何全部查询
服务器托管名词解释
天地心网络技术有限公司
java测试数据库同步
switch 哪个服务器快
宏名深圳网络技术有限公司
数据库原理及应用的意义
思讯商锐9.5数据库在哪
网络技术报考网站
许昌市公安局网络安全支队
装好数据库之后
数据库实验课程总结
剑灵无日峰服务器
db2如何查看数据库编码集
奉贤区网络软件开发服务优势
服务器告警
支付宝网络技术地推
车辆维修软件开发
实时展示数据库
幼儿园网络安全责任制制度
水务行业网络安全方案厂家
环保类软件开发企业
c 中怎么输入多组数据库