1
0
mirror of https://github.com/golang/go synced 2024-09-23 17:20:13 -06:00

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: Dominik Honnef <dominik@honnef.co>
This commit is contained in:
Brad Fitzpatrick 2016-12-01 04:29:12 +00:00
parent 41908a5453
commit 9ea306a10c

View File

@ -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}]
}