example_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package sort_test
  5. import (
  6. "fmt"
  7. "math"
  8. "sort"
  9. )
  10. func ExampleInts() {
  11. s := []int{5, 2, 6, 3, 1, 4} // unsorted
  12. sort.Ints(s)
  13. fmt.Println(s)
  14. // Output: [1 2 3 4 5 6]
  15. }
  16. func ExampleIntsAreSorted() {
  17. s := []int{1, 2, 3, 4, 5, 6} // sorted ascending
  18. fmt.Println(sort.IntsAreSorted(s))
  19. s = []int{6, 5, 4, 3, 2, 1} // sorted descending
  20. fmt.Println(sort.IntsAreSorted(s))
  21. s = []int{3, 2, 4, 1, 5} // unsorted
  22. fmt.Println(sort.IntsAreSorted(s))
  23. // Output: true
  24. // false
  25. // false
  26. }
  27. func ExampleFloat64s() {
  28. s := []float64{5.2, -1.3, 0.7, -3.8, 2.6} // unsorted
  29. sort.Float64s(s)
  30. fmt.Println(s)
  31. s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0} // unsorted
  32. sort.Float64s(s)
  33. fmt.Println(s)
  34. // Output: [-3.8 -1.3 0.7 2.6 5.2]
  35. // [NaN -Inf 0 +Inf]
  36. }
  37. func ExampleFloat64sAreSorted() {
  38. s := []float64{0.7, 1.3, 2.6, 3.8, 5.2} // sorted ascending
  39. fmt.Println(sort.Float64sAreSorted(s))
  40. s = []float64{5.2, 3.8, 2.6, 1.3, 0.7} // sorted descending
  41. fmt.Println(sort.Float64sAreSorted(s))
  42. s = []float64{5.2, 1.3, 0.7, 3.8, 2.6} // unsorted
  43. fmt.Println(sort.Float64sAreSorted(s))
  44. // Output: true
  45. // false
  46. // false
  47. }
  48. func ExampleReverse() {
  49. s := []int{5, 2, 6, 3, 1, 4} // unsorted
  50. sort.Sort(sort.Reverse(sort.IntSlice(s)))
  51. fmt.Println(s)
  52. // Output: [6 5 4 3 2 1]
  53. }
  54. func ExampleSlice() {
  55. people := []struct {
  56. Name string
  57. Age int
  58. }{
  59. {"Gopher", 7},
  60. {"Alice", 55},
  61. {"Vera", 24},
  62. {"Bob", 75},
  63. }
  64. sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
  65. fmt.Println("By name:", people)
  66. sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
  67. fmt.Println("By age:", people)
  68. // Output: By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}]
  69. // By age: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}]
  70. }
  71. func ExampleSliceStable() {
  72. people := []struct {
  73. Name string
  74. Age int
  75. }{
  76. {"Alice", 25},
  77. {"Elizabeth", 75},
  78. {"Alice", 75},
  79. {"Bob", 75},
  80. {"Alice", 75},
  81. {"Bob", 25},
  82. {"Colin", 25},
  83. {"Elizabeth", 25},
  84. }
  85. // Sort by name, preserving original order
  86. sort.SliceStable(people, func(i, j int) bool { return people[i].Name < people[j].Name })
  87. fmt.Println("By name:", people)
  88. // Sort by age preserving name order
  89. sort.SliceStable(people, func(i, j int) bool { return people[i].Age < people[j].Age })
  90. fmt.Println("By age,name:", people)
  91. // Output: By name: [{Alice 25} {Alice 75} {Alice 75} {Bob 75} {Bob 25} {Colin 25} {Elizabeth 75} {Elizabeth 25}]
  92. // By age,name: [{Alice 25} {Bob 25} {Colin 25} {Elizabeth 25} {Alice 75} {Alice 75} {Bob 75} {Elizabeth 75}]
  93. }
  94. func ExampleStrings() {
  95. s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
  96. sort.Strings(s)
  97. fmt.Println(s)
  98. // Output: [Alpha Bravo Delta Go Gopher Grin]
  99. }