golang sort 排序
发布时间:2020-12-16 19:01:23 所属栏目:大数据 来源:网络整理
导读:package mainimport ( "fmt" "sort")func main() { d := []int{5,2,6,3,1,4} // unsorted sort.Sort(sort.IntSlice(d)) fmt.Println(d) // Output:[1 2 3 4 5 6] a := []float64{5.5,2.2,6.6,3.3,1.1,4.4} sort.Sort(sort.Float64Slice(a)) fmt.Println(a) /
package main import ( "fmt" "sort" ) func main() { d := []int{5,2,6,3,1,4} // unsorted sort.Sort(sort.IntSlice(d)) fmt.Println(d) // Output:[1 2 3 4 5 6] a := []float64{5.5,2.2,6.6,3.3,1.1,4.4} sort.Sort(sort.Float64Slice(a)) fmt.Println(a) // Output:[1.1 2.2 3.3 4.4 5.5 6.6] s := []string{"PHP","golang","python","C","Objective-C"} sort.Sort(sort.StringSlice(s)) fmt.Println(s) // Output:[C Objective-C PHP golang python] }
以上为例子,如果需要手工定义规则排序,需要定义该对象的swap函数。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |