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

作者:秋天的春 时间:2023-09-16 23:20:18 

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)
}

来源:https://blog.csdn.net/ffzhihua/article/details/83858464

标签:Golang,sort,排序
0
投稿

猜你喜欢

  • ubuntu中python调用C/C++方法之动态链接库详解

    2023-09-07 19:15:55
  • 用Python 爬取猫眼电影数据分析《无名之辈》

    2023-07-03 17:23:26
  • Bootstrap Multiselect 常用组件实现代码

    2024-05-21 10:14:36
  • Python Django Cookie 简单用法解析

    2022-09-15 15:20:03
  • Python去除PDF水印的实现示例

    2022-08-13 19:26:45
  • Pickle模块中的dump()和load()方法简介

    2023-03-21 04:18:06
  • PyCharm如何导入python项目的方法

    2022-10-23 05:50:35
  • Python3 Loguru输出日志工具的使用

    2021-11-18 04:28:29
  • 如何使用Python实现斐波那契数列

    2021-12-09 08:20:30
  • 网页布局的位置重心与位置间的对比关系

    2007-10-15 19:20:00
  • jquery表单验证使用插件formValidator

    2023-07-02 05:30:54
  • JavaScript 关于引用那点事

    2009-11-28 18:44:00
  • 浅析Python编写函数装饰器

    2023-06-07 12:20:05
  • 如何表示python中的相对路径

    2022-09-06 14:13:41
  • 用Python代码来解图片迷宫的方法整理

    2023-03-17 18:35:43
  • MySQL连接无法解析HOST主机名的解决方法

    2024-01-23 08:38:05
  • 浅谈vue websocket nodeJS 进行实时通信踩到的坑

    2024-04-10 13:46:38
  • Python超简单分析评论提取关键词制作精美词云流程

    2021-07-27 22:00:41
  • MySQL中BIGINT数据类型如何存储整数值

    2024-01-23 21:15:45
  • 用python建立两个Y轴的XY曲线图方法

    2023-06-30 15:01:26
  • asp之家 网络编程 m.aspxhome.com