go标准库-sort

文章展示了Go语言标准库sort的使用,包括Float64s、Ints、Strings函数进行数字和字符串排序,以及Float64sAreSorted、IntsAreSorted、StringsAreSorted检查排序状态,Search系列函数查找特定值的索引,SliceStable和Sort稳定排序自定义类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发环境:win10+go1.18.1

标准库sort的简单使用

0x1、使用

// 数据排序过后的顺序:由小到大
// NaN排在数据之前
func Test_Float64s(t *testing.T) {
	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6}
	fmt.Printf("排序之前:%v \n", s) // 排序之前:[5.2 -1.3 0.7 -3.8 2.6]
	sort.Float64s(s)
	fmt.Printf("排序过后:%v \n", s) // 排序过后:[-3.8 -1.3 0.7 2.6 5.2]

	fmt.Println()
	s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0}
	fmt.Printf("排序之前:%v \n", s) // 排序之前:[+Inf NaN -Inf 0]
	sort.Float64s(s)
	fmt.Printf("排序过后:%v \n", s) // 排序过后:[NaN -Inf 0 +Inf]
}

func Test_Float64sAreSorted(t *testing.T) {
	// Float64sAreSorted检测一个slice是否排序过

	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6}
	fmt.Printf("s 是否排过序 %v\n", sort.Float64sAreSorted(s))
	sort.Float64s(s)
	fmt.Printf("s 是否排过序 %v\n", sort.Float64sAreSorted(s))

}

// 数据排序过后的顺序:由小到大
func Test_Ints(t *testing.T) {
	s := []int{5, 2, 6, 3, 1, 4}
	fmt.Printf("排序之前:%v \n", s) // 排序之前:[5 2 6 3 1 4]
	sort.Ints(s)
	fmt.Printf("排序过后:%v \n", s) // 排序过后:[1 2 3 4 5 6]
}

func Test_IntsAreSorted(t *testing.T) {
	s := []int{5, 2, 6, 3, 1, 4}
	fmt.Printf("s 是否排过序 %v\n", sort.IntsAreSorted(s)) // s 是否排过序 false
	sort.Ints(s)
	fmt.Printf("s 是否排过序 %v\n", sort.IntsAreSorted(s)) // s 是否排过序 true
}

// 数据排序过后的顺序:由小到大
func Test_Strings(t *testing.T) {
	s := []string{"abc", "abd", "bcd", "ab", "a", "ubc"}
	fmt.Printf("排序之前:%v \n", s) // 排序之前:[abc abd bcd ab a ubc]
	sort.Strings(s)
	fmt.Printf("排序过后:%v \n", s) // 排序过后:[a ab abc abd bcd ubc]
}

func Test_StringAreSorted(t *testing.T) {
	s := []string{"abc", "abd", "bcd", "ab", "a", "ubc"}
	fmt.Printf("s 是否排过序 %v\n", sort.StringsAreSorted(s)) // s 是否排过序 false
	sort.Strings(s)
	fmt.Printf("s 是否排过序 %v\n", sort.StringsAreSorted(s)) // s 是否排过序 true
}

func Test_Search(t *testing.T) {
	s := []int{5, 2, 6, 3, 1, 4}
	sort.Ints(s) // 必须经过排序
	fmt.Printf("排序过后:%v \n", s)

	index := sort.Search(len(s), func(i int) bool {
		return s[i] > 3
	})
	fmt.Printf("最小的索引: %d\n", index)
}

func Test_SearchInts(t *testing.T) {
	s := []int{2, 2, 3, 3, 2, 2, 2, 2, 5, 2, 6, 3, 1, 4}
	sort.Ints(s) // 必须经过排序
	fmt.Printf("排序过后:%v \n", s)

	index := sort.SearchInts(s, 2) // 大于等于5的最小索引
	fmt.Printf("最小的索引: %d\n", index)
}

func Test_SearchFloat64(t *testing.T) {
	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6}
	sort.Float64s(s) // 必须经过排序
	fmt.Printf("排序过后:%v \n", s)

	index := sort.SearchFloat64s(s, 5) // 大于等于5的最小索引
	fmt.Printf("最小的索引: %d\n", index)
}

func Test_SearchFloat624(t *testing.T) {
	s := []string{"abc", "abd", "bcd", "ab", "a", "ubc"}

	sort.Strings(s)             // 必须经过排序
	fmt.Printf("排序过后:%v \n", s) // 排序过后:[a ab abc abd bcd ubc]

	index := sort.SearchStrings(s, "ab") // 大于等于5的最小索引
	fmt.Printf("最小的索引: %d\n", index)     // 最小的索引: 1
}

func Test_SliceStable(t *testing.T) {
	s := []int{50, 2, 6, 2, 3, 1, 4}
	sort.SliceStable(s, func(i, j int) bool {
		return s[i] > s[j]
	})

	fmt.Printf("排序过后:%v \n", s) // 排序过后:[50 6 4 3 2 2 1]
}

func Test_Slice(t *testing.T) {
	s := []int{50, 2, 6, 2, 3, 1, 4}
	sort.Slice(s, func(i, j int) bool {
		return s[i] > s[j]
	})

	fmt.Printf("排序过后:%v \n", s) // 排序过后:[50 6 4 3 2 2 1]
}

type A []int

func (p A) Len() int {
	return len(p)
}

func (p A) Less(i, j int) bool {
	return p[i] < p[j]
}

func (p A) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func Test_Stable(t *testing.T) {
	a1 := make(A, 0)
	a1 = append(a1, 44, 5, 3, 2333, 65, 4)

	fmt.Printf("%v \n", sort.IsSorted(a1))

	sort.Stable(a1)

	fmt.Printf("排序过后:%v \n", a1) // 排序过后:[50 6 4 3 2 2 1]

	fmt.Printf("%v \n", sort.IsSorted(a1))

}

func Test_Sort(t *testing.T) {
	a1 := make(A, 0)
	a1 = append(a1, 44, 5, 3, 2333, 65, 4)

	fmt.Printf("%v \n", sort.IsSorted(a1))

	sort.Sort(a1)

	fmt.Printf("排序过后:%v \n", a1) // 排序过后:[50 6 4 3 2 2 1]

	fmt.Printf("%v \n", sort.IsSorted(a1))
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值