diff options
Diffstat (limited to 'src/sort/example_test.go')
-rw-r--r-- | src/sort/example_test.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/sort/example_test.go b/src/sort/example_test.go index f7372bec37..980c0d0368 100644 --- a/src/sort/example_test.go +++ b/src/sort/example_test.go @@ -22,3 +22,22 @@ func ExampleReverse() { fmt.Println(s) // Output: [6 5 4 3 2 1] } + +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) + // Output: By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}] + // By age: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}] +} |