golang: 使用 sort 来排序

转置:https://blog.csdn.net/weixin_41918841/article/details/82898173

golang sort package: https://golang.org/src/sort

sort 操作的对象通常是一个 slice,需要满足三个基本的接口,并且能够使用整数来索引

  1. // A type, typically a collection, that satisfies sort.Interface can be
  2. // sorted by the routines in this package. The methods require that the
  3. // elements of the collection be enumerated by an integer index.
  4. type Interface interface {
  5. // Len is the number of elements in the collection.
  6. Len() int
  7. // Less reports whether the element with
  8. // index i should sort before the element with index j.
  9. Less(i, j int) bool
  10. // Swap swaps the elements with indexes i and j.
  11. Swap(i, j int)
  12. }

ex-1 对 []int 从小到大排序

  1. package main
  2.  
  3. import (
  4.  "fmt"
  5.  "sort"
  6. )
  7.  
  8. type IntSlice []int
  9.  
  10. func (s IntSlice) Len() int { return len(s) }
  11. func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
  12. func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
  13.  
  14. func main() {
  15.  a := []int{4,3,2,1,5,9,8,7,6}
  16.  sort.Sort(IntSlice(a))
  17.  fmt.Println("After sorted: ", a)
  18. }

ex-2 使用 sort.Ints 和 sort.Strings
golang 对常见的 []int 和 []string 分别定义了 IntSlice 和 StringSlice, 实现了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接对 []int 和 []string 进行排序, 使用起来非常方便

  1. package main
  2.  
  3. import (
  4.  "fmt"
  5.  "sort"
  6. )
  7.  
  8. func main() {
  9.  a := []int{3, 5, 4, -1, 9, 11, -14}
  10.  sort.Ints(a)
  11.  fmt.Println(a)
  12.  
  13.  ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"}
  14.  sort.Strings(ss)
  15.  fmt.Println(ss)
  16.  sort.Sort(sort.Reverse(sort.StringSlice(ss)))
  17.  fmt.Printf("After reverse: %v\n", ss)
  18. }
  19.  

ex-3 使用 sort.Reverse 进行逆序排序
如果我们想对一个 sortable object 进行逆序排序,可以自定义一个type。但 sort.Reverse 帮你省掉了这些代码

  1. package main
  2.  
  3. import (
  4.  "fmt"
  5.  "sort"
  6. )
  7.  
  8. func main() {
  9.  a := []int{4,3,2,1,5,9,8,7,6}
  10.  sort.Sort(sort.Reverse(sort.IntSlice(a)))
  11.  fmt.Println("After reversed: ", a)
  12. }

ex-4 使用 sort.Stable 进行稳定排序
sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable

    1. package main
    2.  
    3. import (
    4.  "fmt"
    5.  "sort"
    6. )
    7.  
    8. type person struct {
    9.  Name string
    10.  Age int
    11. }
    12.  
    13. type personSlice []person
    14.  
    15. func (s personSlice) Len() int { return len(s) }
    16. func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    17. func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age }
    18.  
    19. func main() {
    20.  a := personSlice {
    21.  {
    22.  Name: "AAA",
    23.  Age: 55,
    24.  },
    25.  {
    26.  Name: "BBB",
    27.  Age: 22,
    28.  },
    29.  {
    30.  Name: "CCC",
    31.  Age: 0,
    32.  },
    33.  {
    34.  Name: "DDD",
    35.  Age: 22,
    36.  },
    37.  {
    38.  Name: "EEE",
    39.  Age: 11,
    40.  },
    41.  }
    42.  sort.Stable(a)
    43.  fmt.Println(a)
    44. }
posted @ 2020-04-17 15:16  Awakenedy  阅读(269)  评论(0编辑  收藏  举报