ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Go sort包

2022-05-16 17:34:34  阅读:127  来源: 互联网

标签:sort stus return name int func Go


sort包简介

官方文档
Golang的sort包用来排序,二分查找等操作。本文主要介绍sort包里常用的函数,通过实例代码来快速学会使用sort包

sort包内置函数

sort.Ints(x []int)

	ints := []int{1, 4, 3, 2}
	fmt.Printf("%v\n", ints) 
	sort.Ints(ints) //默认升序
	fmt.Printf("%v\n", ints) //[1 2 3 4] 
	sort.Sort(sort.Reverse(sort.IntSlice(ints))) //降序排序 
	fmt.Printf("%v\n", ints) //[4 3 2 1]

sort.Strings(x []string) sort.Float64s(x []float64)

  • 使用方法同上,都是对内置int string float6类型的便捷排序

sort.Slice(x any, less func(i, j int) bool)

  • 传入对象是切片,要自己实现回调函数
	slices := []int{1, 1, 4, 5, 1, 4}
	sort.Slice(slices, func(i, j int) bool {
		return slices[i] < slices[j]
	})
	fmt.Printf("%v\n", slices)//[1 1 1 4 4 5]
  • 同时也可以对结构体自定义排序规则
	type stu struct {
		name string
		age  int
	}

	stus := []stu{{"h", 20}, {"a", 23}, {"h", 21}}
	sort.Slice(stus, func(i, j int) bool {
		if stus[i].name == stus[j].name {
			return stus[i].age > stus[j].age // 年龄逆序
		}
		return stus[i].name < stus[j].name // 名字正序
	})
	fmt.Printf("%v\n", stus) //[{a 23} {h 21} {h 20}]

sort.Sort(data Interface)

  • 自定义排序,需要实现 Len() Less() Swap() 三个方法
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)
}
  • 使用代码
type stu struct {
	name string
	age  int
}
type student []stu

func (s student) Len() int {
	return len(s)
}
func (s student) Less(i, j int) bool {
	if s[i].name == s[j].name {
		return s[i].age > s[j].age // 年龄逆序
	}
	return s[i].name < s[j].name // 名字正序
}
func (s student) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func main() {
	stus1 := student{{"h", 20}, {"a", 23}, {"h", 21}}
	sort.Sort(stus1)
	fmt.Printf("%v\n", stus1) //[{a 23} {h 21} {h 20}] 使用效果等同于sort.Slice
}
  • 使用效果等同于sort.Slice后者代码量较少

sort.SearchInts(a []int, x int) int

  • 该函数是用来二分查找的, 默认是在左边插入
	arr := []int{1, 2, 3, 4, 5, 6, 7}
	idx := sort.SearchInts(arr, 4)
	fmt.Printf("%v\n", idx) // 3

sort.SearchFloat64s(a []float64, x float64) int sort.SearchStrings(a []string, x string) int

  • 这两函数功能同上

sort.Search(n int, f func(int) bool) int

  • 自定义的二分查找,回调函数摇自己实现查找条件
	arr := []int{1, 2, 3, 4, 5, 6, 7}
	idx := sort.Search(len(arr), func(i int) bool {
		return arr[i] > 4
	})
	fmt.Printf("%v\n", idx) //4
  • 相比SearchInts,通过自定义条件便实现了相等情况下在右边插入,前者默认是在左边
  • 更高级一点的用法
	mysring := []string{"abcd", "bcde", "bfag", "cddd"}
	idx := sort.Search(len(mysring), func(i int) bool {
		// 查找头两位字母不是b的,,返回找到的第一个
		return mysring[i][0] != 'b' && mysring[i][1] != 'b'
	})
	fmt.Printf("%v\n", mysring[idx]) // cddd
	mysring := []string{"abcd", "bcde", "bfag", "cddd"}
	idx := sort.Search(len(mysring), func(i int) bool {
		//查找第一个字母不是b的
		return mysring[i][0] <= byte('b')
	})
	fmt.Printf("%v\n", mysring[idx]) // abcd

标签:sort,stus,return,name,int,func,Go
来源: https://www.cnblogs.com/notomatoes/p/16277688.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有