* gcc-interface/decl.c (warn_on_field_placement): Issue the warning
[official-gcc.git] / libgo / go / sort / example_interface_test.go
blob442204ea9eb93949e91df2b5fee7d553c5ce11a4
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package sort_test
7 import (
8 "fmt"
9 "sort"
12 type Person struct {
13 Name string
14 Age int
17 func (p Person) String() string {
18 return fmt.Sprintf("%s: %d", p.Name, p.Age)
21 // ByAge implements sort.Interface for []Person based on
22 // the Age field.
23 type ByAge []Person
25 func (a ByAge) Len() int { return len(a) }
26 func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
27 func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
29 func Example() {
30 people := []Person{
31 {"Bob", 31},
32 {"John", 42},
33 {"Michael", 17},
34 {"Jenny", 26},
37 fmt.Println(people)
38 sort.Sort(ByAge(people))
39 fmt.Println(people)
41 // Output:
42 // [Bob: 31 John: 42 Michael: 17 Jenny: 26]
43 // [Michael: 17 Jenny: 26 Bob: 31 John: 42]