sort.Sort() interface in GoLang
原文:https://medium.com/@kdnotes/sort-sort-interface-in-golang-1d263d96956d
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package main import ( "fmt" "sort" //"unicode/utf8" ) type P struct { name string age int } type nameSortedPs []P func (a nameSortedPs) Len() int { return len(a) } func (a nameSortedPs) Less(i, j int) bool { //iRune, _ := utf8.DecodeRuneInString(a[i].name) //jRune, _ := utf8.DecodeRuneInString(a[j].name) //return int32(iRune) < int32(jRune) return a[i].age > a[j].age } func (a nameSortedPs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func main() { groupA := []P{ { "gold" , 14}, { "dave" , 18}, { "kane" , 12}, { "rain" , 34}, } sort.Sort(nameSortedPs(groupA)) fmt.Println(groupA) } |
------------------------------
Sorting is one of those tasks that comes up from time to time in software project, it important to get just the right algorithm. With the sort package from Go, the Sort interface provides an abstraction on top of commonly used sorting algorithm. That makes it very efficient and for most arbitrary sorting task.
This article briefly explores some sorting methods then explores the GoLang Sort interface in details.
Sorting in Go
sort.Ints
sort.Float64s
sort.Strings
These simple methods can be used to sot a slice of ints, float64 or strings
names := []string{“jane”, “dave”, “mike”, “kane”, “rain”}
sort.Strings(names)
fmt.Println(names)
We can also sort using a common comparative using the sort.Slice method, using the less (i,j) comparative function.
group := []struct {
name string
age int
}{
{“Gold”, 14}, {“dave”, 18}, {“kane”, 12}, {“rain”, 34},
}sort.Slice(group, func(i, j int) bool {
return group[i].age < group[j].age
})fmt.Println(group)
The Sort interface
type Interface interface{
Len() int
Less (i , j) bool
Swap(i , j int)
}
Any collection that implements this interface can be easily sorted. Len returns the length of the collection, Less receives two integers that will serve as indices from the collection, Swap implements the needed change after the Less method is called.
import (
"fmt"
"sort"
"unicode/utf8"
)type P struct {
name string
age int
}type nameSortedPs []Pfunc (a nameSortedPs) Len() int {
return len(a)
}func (a nameSortedPs) Less(i, j int) bool {
iRune, _ := utf8.DecodeRuneInString(a[i].name)
jRune, _ := utf8.DecodeRuneInString(a[j].name)
return int32(iRune) < int32(jRune)
}func (a nameSortedPs) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}func main() {
groupA := []P{
{"gold", 14}, {"dave", 18}, {"kane", 12}, {"rain", 34},
}
sort.Sort(nameSortedPs(groupA))fmt.Println(groupA)
}
The user can implement any logic they wish in regards to what the sort will use to sort on.
By offering the underlying algorithm and defining the sort.Interface
the programming language helps us build powerful sorting programs without enforcing a single opinion on what the algorithm should do but rather how to do it.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-04-20 js利用offsetWidth和clientWidth来计算滚动条的宽度
2018-04-20 procomm plus
2018-04-20 很多shell命令后面的单横杠和双横杠,原来这个意思
2017-04-20 css 實現微信聊天類似的氣泡
2016-04-20 svn代码管理的使用工作流程
2016-04-20 javascript 中this的使用场景全
2016-04-20 HTTPS连接前的几毫秒发生了什么?