diff options
author | Tianon Gravi <admwiggin@gmail.com> | 2015-01-15 11:54:00 -0700 |
---|---|---|
committer | Tianon Gravi <admwiggin@gmail.com> | 2015-01-15 11:54:00 -0700 |
commit | f154da9e12608589e8d5f0508f908a0c3e88a1bb (patch) | |
tree | f8255d51e10c6f1e0ed69702200b966c9556a431 /src/pkg/sort/example_wrapper_test.go | |
parent | 8d8329ed5dfb9622c82a9fbec6fd99a580f9c9f6 (diff) | |
download | golang-upstream/1.4.tar.gz |
Imported Upstream version 1.4upstream/1.4
Diffstat (limited to 'src/pkg/sort/example_wrapper_test.go')
-rw-r--r-- | src/pkg/sort/example_wrapper_test.go | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/src/pkg/sort/example_wrapper_test.go b/src/pkg/sort/example_wrapper_test.go deleted file mode 100644 index cf6d74cf7..000000000 --- a/src/pkg/sort/example_wrapper_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// 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. - -package sort_test - -import ( - "fmt" - "sort" -) - -type Grams int - -func (g Grams) String() string { return fmt.Sprintf("%dg", int(g)) } - -type Organ struct { - Name string - Weight Grams -} - -type Organs []*Organ - -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() { - s := []*Organ{ - {"brain", 1340}, - {"heart", 290}, - {"liver", 1494}, - {"pancreas", 131}, - {"prostate", 62}, - {"spleen", 162}, - } - - sort.Sort(ByWeight{s}) - fmt.Println("Organs by weight:") - printOrgans(s) - - sort.Sort(ByName{s}) - fmt.Println("Organs by name:") - printOrgans(s) - - // Output: - // Organs by weight: - // prostate (62g) - // pancreas (131g) - // spleen (162g) - // heart (290g) - // brain (1340g) - // liver (1494g) - // Organs by name: - // brain (1340g) - // heart (290g) - // liver (1494g) - // pancreas (131g) - // prostate (62g) - // spleen (162g) -} - -func printOrgans(s []*Organ) { - for _, o := range s { - fmt.Printf("%-8s (%v)\n", o.Name, o.Weight) - } -} |