Source file src/sort/example_test.go

     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  
     5  package sort_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"sort"
    11  )
    12  
    13  func ExampleInts() {
    14  	s := []int{5, 2, 6, 3, 1, 4} // unsorted
    15  	sort.Ints(s)
    16  	fmt.Println(s)
    17  	// Output: [1 2 3 4 5 6]
    18  }
    19  
    20  func ExampleIntsAreSorted() {
    21  	s := []int{1, 2, 3, 4, 5, 6} // sorted ascending
    22  	fmt.Println(sort.IntsAreSorted(s))
    23  
    24  	s = []int{6, 5, 4, 3, 2, 1} // sorted descending
    25  	fmt.Println(sort.IntsAreSorted(s))
    26  
    27  	s = []int{3, 2, 4, 1, 5} // unsorted
    28  	fmt.Println(sort.IntsAreSorted(s))
    29  
    30  	// Output: true
    31  	// false
    32  	// false
    33  }
    34  
    35  func ExampleFloat64s() {
    36  	s := []float64{5.2, -1.3, 0.7, -3.8, 2.6} // unsorted
    37  	sort.Float64s(s)
    38  	fmt.Println(s)
    39  
    40  	s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0} // unsorted
    41  	sort.Float64s(s)
    42  	fmt.Println(s)
    43  
    44  	// Output: [-3.8 -1.3 0.7 2.6 5.2]
    45  	// [NaN -Inf 0 +Inf]
    46  }
    47  
    48  func ExampleFloat64sAreSorted() {
    49  	s := []float64{0.7, 1.3, 2.6, 3.8, 5.2} // sorted ascending
    50  	fmt.Println(sort.Float64sAreSorted(s))
    51  
    52  	s = []float64{5.2, 3.8, 2.6, 1.3, 0.7} // sorted descending
    53  	fmt.Println(sort.Float64sAreSorted(s))
    54  
    55  	s = []float64{5.2, 1.3, 0.7, 3.8, 2.6} // unsorted
    56  	fmt.Println(sort.Float64sAreSorted(s))
    57  
    58  	// Output: true
    59  	// false
    60  	// false
    61  }
    62  
    63  func ExampleReverse() {
    64  	s := []int{5, 2, 6, 3, 1, 4} // unsorted
    65  	sort.Sort(sort.Reverse(sort.IntSlice(s)))
    66  	fmt.Println(s)
    67  	// Output: [6 5 4 3 2 1]
    68  }
    69  
    70  func ExampleSlice() {
    71  	people := []struct {
    72  		Name string
    73  		Age  int
    74  	}{
    75  		{"Gopher", 7},
    76  		{"Alice", 55},
    77  		{"Vera", 24},
    78  		{"Bob", 75},
    79  	}
    80  	sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
    81  	fmt.Println("By name:", people)
    82  
    83  	sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
    84  	fmt.Println("By age:", people)
    85  	// Output: By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}]
    86  	// By age: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}]
    87  }
    88  
    89  func ExampleSliceIsSorted() {
    90  	numbers := []int{1, 2, 3, 4, 5, 6}
    91  
    92  	isSortedAsc := sort.SliceIsSorted(numbers, func(i, j int) bool {
    93  		return numbers[i] < numbers[j]
    94  	})
    95  	fmt.Printf("%v sorted ascending: %t\n", numbers, isSortedAsc)
    96  
    97  	numbersDesc := []int{6, 5, 4, 3, 2, 1}
    98  
    99  	isSortedDesc := sort.SliceIsSorted(numbersDesc, func(i, j int) bool {
   100  		return numbersDesc[i] > numbersDesc[j]
   101  	})
   102  	fmt.Printf("%v sorted descending: %t\n", numbers, isSortedDesc)
   103  
   104  	unsortedNumbers := []int{1, 3, 2, 4, 5}
   105  
   106  	isSortedUnsorted := sort.SliceIsSorted(unsortedNumbers, func(i, j int) bool {
   107  		return unsortedNumbers[i] < unsortedNumbers[j]
   108  	})
   109  	fmt.Printf("%v unsorted slice sorted: %t\n", unsortedNumbers, isSortedUnsorted)
   110  
   111  	// Output:
   112  	// [1 2 3 4 5 6] sorted ascending: true
   113  	// [1 2 3 4 5 6] sorted descending: true
   114  	// [1 3 2 4 5] unsorted slice sorted: false
   115  }
   116  
   117  func ExampleSliceStable() {
   118  
   119  	people := []struct {
   120  		Name string
   121  		Age  int
   122  	}{
   123  		{"Alice", 25},
   124  		{"Elizabeth", 75},
   125  		{"Alice", 75},
   126  		{"Bob", 75},
   127  		{"Alice", 75},
   128  		{"Bob", 25},
   129  		{"Colin", 25},
   130  		{"Elizabeth", 25},
   131  	}
   132  
   133  	// Sort by name, preserving original order
   134  	sort.SliceStable(people, func(i, j int) bool { return people[i].Name < people[j].Name })
   135  	fmt.Println("By name:", people)
   136  
   137  	// Sort by age preserving name order
   138  	sort.SliceStable(people, func(i, j int) bool { return people[i].Age < people[j].Age })
   139  	fmt.Println("By age,name:", people)
   140  
   141  	// Output: By name: [{Alice 25} {Alice 75} {Alice 75} {Bob 75} {Bob 25} {Colin 25} {Elizabeth 75} {Elizabeth 25}]
   142  	// By age,name: [{Alice 25} {Bob 25} {Colin 25} {Elizabeth 25} {Alice 75} {Alice 75} {Bob 75} {Elizabeth 75}]
   143  }
   144  
   145  func ExampleStrings() {
   146  	s := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
   147  	sort.Strings(s)
   148  	fmt.Println(s)
   149  	// Output: [Alpha Bravo Delta Go Gopher Grin]
   150  }
   151  

View as plain text