| |
| |
| |
|
|
| package sort_test |
|
|
| import ( |
| "fmt" |
| "math" |
| "sort" |
| ) |
|
|
| func ExampleInts() { |
| s := []int{5, 2, 6, 3, 1, 4} |
| sort.Ints(s) |
| fmt.Println(s) |
| |
| } |
|
|
| func ExampleIntsAreSorted() { |
| s := []int{1, 2, 3, 4, 5, 6} |
| fmt.Println(sort.IntsAreSorted(s)) |
|
|
| s = []int{6, 5, 4, 3, 2, 1} |
| fmt.Println(sort.IntsAreSorted(s)) |
|
|
| s = []int{3, 2, 4, 1, 5} |
| fmt.Println(sort.IntsAreSorted(s)) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleFloat64s() { |
| s := []float64{5.2, -1.3, 0.7, -3.8, 2.6} |
| sort.Float64s(s) |
| fmt.Println(s) |
|
|
| s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0} |
| sort.Float64s(s) |
| fmt.Println(s) |
|
|
| |
| |
| } |
|
|
| func ExampleFloat64sAreSorted() { |
| s := []float64{0.7, 1.3, 2.6, 3.8, 5.2} |
| fmt.Println(sort.Float64sAreSorted(s)) |
|
|
| s = []float64{5.2, 3.8, 2.6, 1.3, 0.7} |
| fmt.Println(sort.Float64sAreSorted(s)) |
|
|
| s = []float64{5.2, 1.3, 0.7, 3.8, 2.6} |
| fmt.Println(sort.Float64sAreSorted(s)) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleReverse() { |
| s := []int{5, 2, 6, 3, 1, 4} |
| sort.Sort(sort.Reverse(sort.IntSlice(s))) |
| fmt.Println(s) |
| |
| } |
|
|
| func ExampleSlice() { |
| people := []struct { |
| Name string |
| Age int |
| }{ |
| {"Gopher", 7}, |
| {"Alice", 55}, |
| {"Vera", 24}, |
| {"Bob", 75}, |
| } |
| sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name }) |
| fmt.Println("By name:", people) |
|
|
| sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age }) |
| fmt.Println("By age:", people) |
| |
| |
| } |
|
|
| func ExampleSliceIsSorted() { |
| numbers := []int{1, 2, 3, 4, 5, 6} |
|
|
| isSortedAsc := sort.SliceIsSorted(numbers, func(i, j int) bool { |
| return numbers[i] < numbers[j] |
| }) |
| fmt.Printf("%v sorted ascending: %t\n", numbers, isSortedAsc) |
|
|
| numbersDesc := []int{6, 5, 4, 3, 2, 1} |
|
|
| isSortedDesc := sort.SliceIsSorted(numbersDesc, func(i, j int) bool { |
| return numbersDesc[i] > numbersDesc[j] |
| }) |
| fmt.Printf("%v sorted descending: %t\n", numbers, isSortedDesc) |
|
|
| unsortedNumbers := []int{1, 3, 2, 4, 5} |
|
|
| isSortedUnsorted := sort.SliceIsSorted(unsortedNumbers, func(i, j int) bool { |
| return unsortedNumbers[i] < unsortedNumbers[j] |
| }) |
| fmt.Printf("%v unsorted slice sorted: %t\n", unsortedNumbers, isSortedUnsorted) |
|
|
| |
| |
| |
| |
| } |
|
|
| func ExampleSliceStable() { |
|
|
| people := []struct { |
| Name string |
| Age int |
| }{ |
| {"Alice", 25}, |
| {"Elizabeth", 75}, |
| {"Alice", 75}, |
| {"Bob", 75}, |
| {"Alice", 75}, |
| {"Bob", 25}, |
| {"Colin", 25}, |
| {"Elizabeth", 25}, |
| } |
|
|
| |
| sort.SliceStable(people, func(i, j int) bool { return people[i].Name < people[j].Name }) |
| fmt.Println("By name:", people) |
|
|
| |
| sort.SliceStable(people, func(i, j int) bool { return people[i].Age < people[j].Age }) |
| fmt.Println("By age,name:", people) |
|
|
| |
| |
| } |
|
|
| func ExampleStrings() { |
| s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"} |
| sort.Strings(s) |
| fmt.Println(s) |
| |
| } |
|
|