1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:20:07 -07:00

sort: move example to package level and simplify further

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/13634044
This commit is contained in:
Andrew Gerrand 2013-09-16 13:02:01 +10:00
parent f43b6d470e
commit 49eeef5927

View File

@ -20,18 +20,18 @@ func (p Person) String() string {
// ByAge implements sort.Interface for []Person based on // ByAge implements sort.Interface for []Person based on
// the Age field. // the Age field.
type ByAge []*Person type ByAge []Person
func (a ByAge) Len() int { return len(a) } func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age } func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func ExampleInterface() { func Example() {
people := []*Person{ people := []Person{
&Person{Name: "Bob", Age: 31}, {"Bob", 31},
&Person{Name: "John", Age: 42}, {"John", 42},
&Person{Name: "Michael", Age: 17}, {"Michael", 17},
&Person{Name: "Jenny", Age: 26}, {"Jenny", 26},
} }
fmt.Println(people) fmt.Println(people)