golang编程开发使用sort排序示例详解

 更新时间:2021年11月15日 16:58:49   作者:秋天的春  
这篇文章主要为大家介绍了go语言编程开发使用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) 
}

以上就是go语言编程使用sort来排序示例详解的详细内容,更多关于go语言sort排序的资料请关注脚本之家其它相关文章!

相关文章

  • Go标准库http与fasthttp服务端性能对比场景分析

    Go标准库http与fasthttp服务端性能对比场景分析

    这篇文章主要介绍了Go标准库http与fasthttp服务端性能比较,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Go Excelize API源码解析GetSheetFormatPr使用示例

    Go Excelize API源码解析GetSheetFormatPr使用示例

    这篇文章主要为大家介绍了Go Excelize API源码解析GetSheetFormatPr使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Golang内存泄漏场景以及解决方案详析

    Golang内存泄漏场景以及解决方案详析

    golang中内存泄露的发现与排查一直是来是go开发者头疼的一件事,下面这篇文章主要给大家介绍了关于Golang内存泄漏场景以及解决的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • go:gin输出日志文件方式

    go:gin输出日志文件方式

    这篇文章主要介绍了go:gin输出日志文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • GO语言开发终端命令行小工具改进更新

    GO语言开发终端命令行小工具改进更新

    这篇文章主要为大家介绍了GO语言开发终端命令行小工具的改进更新,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Go1.21新增内置函数(built-in functions)详解

    Go1.21新增内置函数(built-in functions)详解

    Go 1.21新增的内置函数分别是 min、max 和 clear,这篇文章主要带大家一起了解一下这几个函数的用途和使用示例,感兴趣的小伙伴可以学习一下
    2023-08-08
  • Go每日一库之zap日志库的安装使用指南

    Go每日一库之zap日志库的安装使用指南

    这篇文章主要为大家介绍了Go每日一库之zap安装使用示例学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • 一篇文章带你搞懂Go语言标准库Time

    一篇文章带你搞懂Go语言标准库Time

    在我们开发的过程中,每个项目都需要时间这一类的函数,此时对time这个包的研究深度就显得尤为重要,这篇文章主要给大家介绍了关于如何通过一篇文章带你搞懂Go语言标准库Time的相关资料,需要的朋友可以参考下
    2022-10-10
  • go开发中引用静态库.a文件的方法

    go开发中引用静态库.a文件的方法

    这篇文章主要介绍了go开发中引用静态库.a文件的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Go语言特点及基本数据类型使用详解

    Go语言特点及基本数据类型使用详解

    这篇文章主要为大家介绍了Go语言特点及基本数据类型使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03

最新评论