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.
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
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
}
38 sort
.Sort(ByAge(people
))
42 // [Bob: 31 John: 42 Michael: 17 Jenny: 26]
43 // [Michael: 17 Jenny: 26 Bob: 31 John: 42]