Commit 9ea306a1 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

sort: add Slice example

Change-Id: I34ba4eaf1d232b639998ad3bbb0d075dd097722b
Reviewed-on: https://go-review.googlesource.com/33763
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarDominik Honnef <dominik@honnef.co>
parent 41908a54
......@@ -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}]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment