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.
14 func (g Grams) String() string { return fmt.Sprintf("%dg", int(g)) }
23 func (s Organs) Len() int { return len(s) }
24 func (s Organs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
26 // ByName implements sort.Interface by providing Less and using the Len and
27 // Swap methods of the embedded Organs value.
28 type ByName struct{ Organs }
30 func (s ByName) Less(i, j int) bool { return s.Organs[i].Name < s.Organs[j].Name }
32 // ByWeight implements sort.Interface by providing Less and using the Len and
33 // Swap methods of the embedded Organs value.
34 type ByWeight struct{ Organs }
36 func (s ByWeight) Less(i, j int) bool { return s.Organs[i].Weight < s.Organs[j].Weight }
38 func Example_sortWrapper() {
48 sort.Sort(ByWeight{s})
49 fmt.Println("Organs by weight:")
53 fmt.Println("Organs by name:")
73 func printOrgans(s []*Organ) {
75 fmt.Printf("%-8s (%v)\n", o.Name, o.Weight)