golang sort 排序

前端之家收集整理的这篇文章主要介绍了golang sort 排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7.  
  8. func main() {
  9. d := []int{5,2,6,3,1,4} // unsorted
  10. sort.Sort(sort.IntSlice(d))
  11. fmt.Println(d)
  12. // Output:[1 2 3 4 5 6]
  13.  
  14. a := []float64{5.5,2.2,6.6,3.3,1.1,4.4}
  15. sort.Sort(sort.Float64Slice(a))
  16. fmt.Println(a)
  17. // Output:[1.1 2.2 3.3 4.4 5.5 6.6]
  18.  
  19. s := []string{"PHP","golang","python","C","Objective-C"}
  20. sort.Sort(sort.StringSlice(s))
  21. fmt.Println(s)
  22. // Output:[C Objective-C PHP golang python]
  23. }


以上为例子,如果需要手工定义规则排序,需要定义该对象的swap函数

猜你在找的Go相关文章