您现在的位置是:群英 > 开发技术 > 编程语言
Golang使用sort排序怎么实现,代码是什么
Admin发表于 2022-07-06 17:12:19484 次浏览
这篇文章给大家介绍了“Golang使用sort排序怎么实现,代码是什么”的相关知识,讲解详细,步骤过程清晰,对大家进一步学习和理解“Golang使用sort排序怎么实现,代码是什么”有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。

golang sort package: https://studygolang.com/articles/3360

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 []int
func (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 string
Age 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排序怎么实现,代码是什么的介绍啦,需要的朋友可以参考上述内容,希望对大家有帮助,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-05-19 17:39:52 
摘要:在go语言中,可以利用“Slice(切片)”追加元素的特性完成元素的删除操作,通过内建函数append()实现对单个元素以及元素片段的删除;语法“append(list[:删除索引], list[(删除索引+1):]...)”。
2022-09-20 18:12:30 
摘要:这篇文章主要给大家介绍了关于php图片缩放和裁剪的相关资料,主要利用的是imagecopyresampled(),文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
2022-07-25 17:55:02 
摘要:bootstrap icon不显示的解决办法:首先找到fonts文件夹下的指定文件;然后保证该文件和bootstrap.css文件的相对路径正确即可。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部