| // Copyright 2011 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| func (g Grams) String() string { return fmt.Sprintf("%dg", int(g)) } |
| func (s Organs) Len() int { return len(s) } |
| func (s Organs) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| // ByName implements sort.Interface by providing Less and using the Len and |
| // Swap methods of the embedded Organs value. |
| type ByName struct{ Organs } |
| func (s ByName) Less(i, j int) bool { return s.Organs[i].Name < s.Organs[j].Name } |
| // ByWeight implements sort.Interface by providing Less and using the Len and |
| // Swap methods of the embedded Organs value. |
| type ByWeight struct{ Organs } |
| func (s ByWeight) Less(i, j int) bool { return s.Organs[i].Weight < s.Organs[j].Weight } |
| func Example_sortWrapper() { |
| fmt.Println("Organs by weight:") |
| fmt.Println("Organs by name:") |
| func printOrgans(s []*Organ) { |
| fmt.Printf("%-8s (%v)\n", o.Name, o.Weight) |