golang之切⽚与排序
1.排序与查操作
排序操作在sort包中,sort.Ints对整数进⾏排序,sort.Strings对字符串进⾏排序,sort.Float64对浮点数进⾏排序package main
import (
"fmt"
"sort"
)
func testIntSort() {
var a = [...]int{1, 8, 38, 2, 348, 484}
//数组是值类型,不能直接排序,必须转为切⽚
sort.Ints(a[:])
fmt.Println(a)go 字符串转数组
}
func testStrings() {
var a = [...]string{"abc", "efg", "b", "A", "eeee"}
//按照字母顺序排序,从⼩到⼤
sort.Strings(a[:])
fmt.Println(a)
}
func testFloat() {
var a = [...]float64{2.3, 0.8, 28.2, 392342.2, 0, 6}
//从⼩到⼤排序
sort.Float64s(a[:])
fmt.Println(a)
}
func testIntSearch() {
var a = [...]int{1, 8, 38, 2, 348, 484}
//数组是值类型,不能直接排序,必须转为切⽚
sort.Ints(a[:])
//SearchInts默认排序后的位置,⼀定要排序后在查
index:=sort.SearchInts(a[:],348)
fmt.Println(index)
}
func main() {
testIntSort()
testStrings()
testFloat()
testIntSearch()
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。