golang 排序库使用的方法
package main
import (
"fmt"
"sort"
)
func main() {
var w = []float64{1, 2, 3}
var w1 = []int{1, 2, 3}
sort.Float64s(w)
fmt.Println(w)
sort.Ints(w1)
fmt.Println(w1)
var w2 = []int64{33, 2, 3}
sort.Slice(w2[:], func(i, j int) bool {
return w2[i] < w2[j]
})
sort.SliceStable(w2[:], func(i, j int) bool {
return w2[i] > w2[j]
})
fmt.Println(w2)
//二分查找演示
var res = sort.Search(len(w2), func(i int) bool {
return w2[i] == int64(3)
})
fmt.Println(res)
res = sort.Search(len(w2), func(i int) bool {
return w2[i] == int64(34)
})
fmt.Println(res)
}