sort 包 在内部实现了四种基本的排序算法:插入排序(insertionSort)、归并排序(symMerge)、堆排序(heapSort)和快速排序(quickSort); sort 包会依据实际数据自动选择最优的排序算法。所以我们写代码时只需要考虑实现 sort.Interface 这个类型就可以了。

    1. func Sort(data Interface) {
    2. // Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
    3. n := data.Len()
    4. maxDepth := 0
    5. for i := n; i > 0; i >>= 1 {
    6. maxDepth++
    7. }
    8. maxDepth *= 2
    9. quickSort(data, 0, n, maxDepth)
    10. }
    11. type Interface interface {
    12. // Len is the number of elements in the collection.
    13. Len() int
    14. // Less reports whether the element with
    15. // index i should sort before the element with index j.
    16. Less(i, j int) bool
    17. // Swap swaps the elements with indexes i and j.
    18. Swap(i, j int)
    19. }
    20. // 内部实现的四种排序算法
    21. // 插入排序
    22. func insertionSort(data Interface, a, b int)
    23. // 堆排序
    24. func heapSort(data Interface, a, b int)
    25. // 快速排序
    26. func quickSort(data Interface, a, b, maxDepth int)
    27. // 归并排序
    28. func symMerge(data Interface, a, m, b int)

    调用 sort.Sort() 来实现自定义类型排序,只需要我们的类型实现 Interface 接口类型中的三个方法即可。
    ##sort 包本身对于 []int 类型如何排序

    1. type IntSlice []int
    2. // 获取此 slice 的长度
    3. func (p IntSlice) Len() int { return len(p) }
    4. // 比较两个元素大小 升序
    5. func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
    6. // 交换数据
    7. func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    8. // sort.Ints()内部调用Sort() 方法实现排序
    9. // 注意 要先将[]int 转换为 IntSlice类型 因为此类型才实现了Interface的三个方法
    10. func Ints(a []int) { Sort(IntSlice(a)) }
    1. package main
    2. import (
    3. "fmt"
    4. "sort"
    5. "strings"
    6. )
    7. type StuScore struct {
    8. name string
    9. score int
    10. }
    11. type StuScores []StuScore
    12. func (s StuScores) Len() int {
    13. return len(s)
    14. }
    15. func (s StuScores) Less(x, y int) bool {
    16. return s[x].score < s[y].score
    17. }
    18. func (s StuScores) Swap(x, y int) {
    19. s[x], s[y] = s[y], s[x]
    20. }
    21. type SortByName struct {
    22. StuScores
    23. }
    24. func (s SortByName) Less(x, y int) bool {
    25. if strings.Compare(s.StuScores[x].name, s.StuScores[y].name) < 1 {
    26. return true
    27. }
    28. return false
    29. }
    30. type SortByAgeDESC struct {
    31. StuScores
    32. }
    33. func (s SortByAgeDESC) Less(x, y int) bool {
    34. return s.StuScores[x].score > s.StuScores[y].score
    35. }
    36. func main() {
    37. stus := StuScores{{"name2", 95}, {"name1", 75}, {"name5", 86}, {"name4", 60}, {"name3", 100}}
    38. fmt.Println("排序前------------------")
    39. for _, v := range stus {
    40. fmt.Println(v.name, ":", v.score)
    41. }
    42. fmt.Println("按照分数升序排序------------------")
    43. sort.Sort(stus)
    44. for _, v := range stus {
    45. fmt.Println(v.name, ":", v.score)
    46. }
    47. fmt.Println("按照姓名排序------------------")
    48. sort.Sort(SortByName{stus})
    49. for _, v := range stus {
    50. fmt.Println(v.name, ":", v.score)
    51. }
    52. fmt.Println("按照分数降序排序------------------")
    53. sort.Sort(SortByAgeDESC{stus})
    54. for _, v := range stus {
    55. fmt.Println(v.name, ":", v.score)
    56. }
    57. }

    当然不是。我们可以利用嵌套结构体来解决这个问题。因为嵌套结构体可以继承父结构体的所有属性和方法;

    1. type SortByName struct {
    2. StuScores
    3. }
    4. func (s SortByName) Less(x, y int) bool {
    5. if strings.Compare(s.StuScores[x].name, s.StuScores[y].name) < 1 {
    6. return true
    7. }
    8. return false
    9. }
    10. type SortByAgeDESC struct {
    11. StuScores
    12. }
    13. func (s SortByAgeDESC) Less(x, y int) bool {
    14. return s.StuScores[x].score > s.StuScores[y].score
    15. }