go语言的魔幻旅程22-sort包

生存还是毁灭,这是个问题

最近无意中刷到华为创始人任正非的一则访谈视频,视频中任老板首先是针对某位房产大佬的小目标进行了一番批判,然后话锋一转,批判在金钱至上的社会中要能忍得住浮躁,不为眼前产生的泡沫所诱惑,任老板的话可谓一针见血。在经过几十年的发展之后,绝大部分的人都摆脱了过去吃不饱,睡不暖的穷苦日子,然则要想满足额外的物质享受,光靠工资这种单一的收入来源显然不能满足所需,在此情形之下,币圈一日,人间一年的神话不断上演,每个人都想趁机捞一笔,但是有赢家必定有输家,最大的赢家无非是交易所,毕竟人家不是做慈善的,不过还是像任老板所说的那样,越是穷苦,越是需要干坐冷板凳,踏踏实实的奋斗,不一头扎入泡沫中。

go语言的sort包

go语言的sort包的作用是对元素进行按照一定的规则对元素进行相关的排序,下面主要针对sort包中的函数和方法展开讲解。

package main

/*********************************************************************/
/**************** golang中sort包相关API讲解 ************************/
/*******************************************************************/

/*
func Float64s(x []float64)
func Float64sAreSorted(x []float64) bool
func Ints(x []int)
func IntsAreSorted(x []int) bool
func IsSorted(data Interface) bool
func Search(n int, f func(int) bool) int
func SearchFloat64s(a []float64, x float64) int
func SearchInts(a []int, x int) int
func SearchStrings(a []string, x string) int
func Slice(x interface{}, less func(i, j int) bool)
func SliceIsSorted(x interface{}, less func(i, j int) bool) bool
func SliceStable(x interface{}, less func(i, j int) bool)
func Sort(data Interface)
func Stable(data Interface)
func Strings(x []string)
func StringsAreSorted(x []string) bool
type Float64Slice
	func (x Float64Slice) Len() int
	func (x Float64Slice) Less(i, j int) bool
	func (p Float64Slice) Search(x float64) int
	func (x Float64Slice) Sort()
	func (x Float64Slice) Swap(i, j int)
type IntSlice
	func (x IntSlice) Len() int
	func (x IntSlice) Less(i, j int) bool
	func (p IntSlice) Search(x int) int
	func (x IntSlice) Sort()
	func (x IntSlice) Swap(i, j int)
type Interface
	func Reverse(data Interface) Interface
type StringSlice
	func (x StringSlice) Len() int
	func (x StringSlice) Less(i, j int) bool
	func (p StringSlice) Search(x string) int
	func (x StringSlice) Sort()
	func (x StringSlice) Swap(i, j int)
*/


func main() {

	/**
	*Float64s按递增顺序对float64s的一部分进行排序。 Not-a-number(NaN)值先于其他值排序。
	*func Float64s(x []float64)
	*/

	/*
	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6}
	sort.Float64s(s)
	fmt.Println(s)

	s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0}
	sort.Float64s(s)
	fmt.Println(s)
	*/

	/**
	*Float64sAreSorted报告切片x是否以递增的顺序排序,其中not-a-number(NaN)值位于任何其他值之前。
	*func Float64sAreSorted(x []float64) bool
	*/

	/*
	s := []float64{0.7, 1.3, 2.6, 3.8, 5.2}
	fmt.Println(sort.Float64sAreSorted(s))

	s = []float64{5.2, 3.8, 2.6, 1.3, 0.7}
	fmt.Println(sort.Float64sAreSorted(s))

	s = []float64{5.2, 1.3, 0.7, 3.8, 2.6}
	fmt.Println(sort.Float64sAreSorted(s))
	*/


	/**
	*整数按升序对一片整数进行排序
	*func Ints(x []int)
	*/

	/*
	s := []int{5, 2, 6, 3, 1, 4}
	sort.Ints(s)
	fmt.Println(s)
	*/

	/**
	*IntsAreSorted报告切片x是否按升序排序。
	*func IntsAreSorted(x []int) bool
	*/

	/*
	s := []int{1, 2, 3, 4, 5, 6}
	fmt.Println(sort.IntsAreSorted(s))

	s = []int{6, 5, 4, 3, 2, 1}
	fmt.Println(sort.IntsAreSorted(s))

	s = []int{3, 2, 4, 1, 5}
	fmt.Println(sort.IntsAreSorted(s))
	*/

	/**
	*IntsAreSorted报告切片x是否按升序排序。
	*func IsSorted(data Interface) bool
	*/

	/*
	type IntSlice []int
	a := IntSlice{1, 3, 5, 7, 2}
	fmt.Println(sort.IsSorted(a))
	*/

	/**
	*search使用二分法进行查找
	*func Search(n int, f func(int) bool) int
	*/

	/*
	a := []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
	x := 6

	i := sort.Search(len(a), func(i int) bool { return a[i] >= x })
	if i < len(a) && a[i] == x {
		fmt.Printf("found %d at index %d in %v\n", x, i, a)
	} else {
		fmt.Printf("%d not found in %v\n", x, a)
	}
	*/

	/**
	*SearchFloat64s 在float64s切片中搜索x并返回索引如Search函数所述.
	*返回可以插入x值的索引位置,如果x不存在,返回值是x应该插入a的位置
	*(以保证a的递增顺序
	*func SearchFloat64s(a []float64, x float64) int
	*/

	/*
	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6}
	index := sort.SearchFloat64s(s, 2)
	fmt.Println(index)
	*/

	/**
	*返回x在a中应该存在的位置,无论a中是否存在a中在递增顺序的a中搜索x,返回x的索引。
	*如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)
	*func SearchInts(a []int, x int) int
	*/

	/*
	s := []int{5, 2, 6, 3, 1, 4}
	index := sort.SearchInts(s, 5)
	fmt.Println(index)
	*/

	/**
	*返回x在a中应该存在的位置,无论a中是否存在a中, 在递增顺序的a中搜索x,返回x的索引。
	*如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)
	*func SearchStrings(a []string, x string) int
	*/

	/*
	s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
	index := sort.SearchStrings(s, "Hello")
	fmt.Println(index)
	*/

	/**
	*切片根据给定的less函数对切片x进行排序。如果x不是切片,它会发生panic,
	*不能保证排序是稳定的:相等的元素可能与其原始顺序相反。对于稳定的排序,
	*请使用SliceStable
	*func Slice(x interface{}, less func(i, j int) bool)
	*/

	/*
	people := []struct {
		Name string
		Age  int
	}{
		{"Gopher", 7},
		{"Alice", 55},
		{"Vera", 24},
		{"Bob", 75},
	}
	sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
	fmt.Println("By name:", people)

	sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
	fmt.Println("By age:", people)
	*/


	/**
	*SliceIsSorted报告切片x是否根据提供的less函数排序。如果x不是切片,它会发生panic
	*func SliceIsSorted(x interface{}, less func(i, j int) bool) bool
	*/

	/*
	people := []struct {
		Name string
		Age  int
	}{
		{"Alice", 25},
		{"Elizabeth", 75},
		{"Alice", 75},
		{"Bob", 75},
		{"Alice", 75},
		{"Bob", 25},
		{"Colin", 25},
		{"Elizabeth", 25},
	}

	sort.SliceIsSorted(people, func(i, j int) bool {
		return people[i].Name < people[j].Name})
	fmt.Println("By name:", people)

	sort.SliceIsSorted(people, func(i, j int) bool {
		return people[i].Age < people[j].Age })
	fmt.Println("By age,name:", people)
	*/


	/**
	*SliceStable使用所提供的less函数对slice x进行排序,以其原始顺序保留相等的元素。
	*如果x不是切片,它会发生panic。
	*func SliceStable(x interface{}, less func(i, j int) bool)
	*/

	/*
	people := []struct {
		Name string
		Age  int
	}{
		{"Alice", 25},
		{"Elizabeth", 75},
		{"Alice", 75},
		{"Bob", 75},
		{"Alice", 75},
		{"Bob", 25},
		{"Colin", 25},
		{"Elizabeth", 25},
	}

	sort.SliceStable(people, func(i, j int) bool {
		return people[i].Name < people[j].Name })
	fmt.Println("By name:", people)

	sort.SliceStable(people, func(i, j int) bool {
		return people[i].Age < people[j].Age })
	fmt.Println("By age,name:", people)
	*/


	/**
	*排序对数据进行排序。它对data.Len进行一次调用,以确定对data的n和O(n * log(n)调用。
	*Less和data.Swap。不能保证排序是稳定的。
	*func Sort(data Interface)
	*/

	/*
	s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
	sort.Sort(sort.StringSlice(s))
	fmt.Println(s)
	*/

	/**
	*排序对数据进行排序。它对data.Len进行一次调用,以确定对data的n和O(n * log(n)调用。
	*func Stable(data Interface)
	*/

	/*
	type People struct {
		Name string
		Age  int
	}

	type Peoples []People

	peoples := Peoples{
		{"Gopher", 7},
		{"Alice", 55},
		{"Vera", 24},
		{"Bob", 75},
	}

	sort.Stable(peoples)
	fmt.Println(peoples)
	*/

	/**
	*字符串按升序对字符串的一部分进行排序。
	*func Strings(x []string)
	*/

	/*
	s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
	sort.Strings(s)
	fmt.Println(s)
	*/

	/**
	*检查是否已排序为递增顺序
	*func StringsAreSorted(x []string) bool
	*/

	/*
	s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
	fmt.Println(sort.StringsAreSorted(s))
	*/

	/*********************************************************************/
	/**************** Float64Slice中相关方法讲解 ************************/
	/*******************************************************************/

	/**
	*求FloatSlice的数据类型的长度
	*func (x Float64Slice) Len() int
	*/

	/*
	var x sort.Float64Slice
	x = []float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), 
        math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
	fmt.Println(x.Len())
	*/

	/**
	*比较i,j位置两个数的大小
	*func (x Float64Slice) Less(i, j int) bool
	*/

	/*
	var x sort.Float64Slice
	x = []float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), 
        math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
	fmt.Println(x.Less(0, 1))
	*/


	/**
	*查找指定元素的位置
	*func (p Float64Slice) Search(x float64) int
	*/

	/*
	var x sort.Float64Slice
	x = []float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), 
        math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
	fmt.Println(x.Search(59.0))
	*/


	/**
	*针对Float64Slice进行排序
	*func (x Float64Slice) Sort()
	*/

	/*
	var x sort.Float64Slice
	x = []float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), 
        math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
	x.Sort()
	fmt.Println(x)
	*/

	/**
	*针对指定位置的元素进行交换
	*func (x Float64Slice) Swap(i, j int)
	*/

	/*
	var x sort.Float64Slice
	x = []float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), 
        math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
	x.Swap(0, 1)
	fmt.Println(x)
	*/

	/*********************************************************************/
	/**************** IntSlice中相关方法讲解 *****************************/
	/********************************************************************/

	/**
	*求IntSlice类型的长度
	*func (x IntSlice) Len() int
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	fmt.Println(x.Len())
	*/

	/**
	*
	*func (x IntSlice) Less(i, j int) bool
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	fmt.Println(x.Less(1, 2))
	*/

	/**
	*根据索引查找指定的元素
	*func (p IntSlice) Search(x int) int
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	fmt.Println(x.Search(1))
	*/


	/**
	*针对x进行排序
	*func (x IntSlice) Sort()
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	x.Sort()
	fmt.Println(x)
	*/

	/**
	*交换指定元素的位置
	*func (x IntSlice) Swap(i, j int)
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	x.Swap(0, 1)
	fmt.Println(x)
	*/

	/*********************************************************************/
	/**************** Interface中相关方法讲解 ***************************/
	/*******************************************************************/

	/**
	*针对元素进行逆序排列
	*func Reverse(data Interface) Interface
	*/

	/*
	var x sort.IntSlice
	x = []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
	sort.Reverse(x)
	fmt.Println(x)
	*/


	/*********************************************************************/
	/**************** StringSlice中相关方法讲解 **************************/
	/********************************************************************/

	/**
	*求StringSlice类型的长度
	*func (x StringSlice) Len() int
	*/

	/*
	var x sort.StringSlice
	x = []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
	fmt.Println(x.Len())
	*/

	/**
	*判断i索引的数据小于j索引的数据返回true
	*func (x StringSlice) Less(i, j int) bool
	*/

	/*
	var x sort.StringSlice
	x = []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
	fmt.Println(x.Less(0, 1))
	*/

	/**
	*查找x的位置
	*func (p StringSlice) Search(x string) int
	*/

	/*
	var x sort.StringSlice
	x = []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
	fmt.Println(x.Search("hello"))
	*/


	/**
	*针对StringSlice进行排序
	*func (x StringSlice) Sort()
	*/

	/*
	var x sort.StringSlice
	x = []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
	x.Sort()
	fmt.Println(x)
	*/


	/**
	*交换指定索引位置的元素
	*func (x StringSlice) Swap(i, j int)
	*/

	/*
	var x sort.StringSlice
	x = []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
	x.Swap(0, 1)
	fmt.Println(x)
	*/
}

小结

sort部分的内容相对而言还是比较少的,但是少不代表不重要,在某些日常开发的场景中我们总会遇到需要进行排序的情况,所以sort包的内容还是务必需要掌握的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值