开发环境: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))
}