From 9ea306a10ca4d25672641ffbb628718a6665b4f3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 1 Dec 2016 04:29:12 +0000 Subject: [PATCH] sort: add Slice example Change-Id: I34ba4eaf1d232b639998ad3bbb0d075dd097722b Reviewed-on: https://go-review.googlesource.com/33763 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Dominik Honnef --- src/sort/example_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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}] +}