From 6e3a40dd04cd9b9df6562c0e58d83034be1c68d2 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sun, 6 Nov 2016 11:17:29 -0800 Subject: [PATCH] move sort func into separate file --- main.go | 32 ++++++++------------------------ sort.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 sort.go diff --git a/main.go b/main.go index 219f9d3..b8dc1e6 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,14 @@ func main() { } } +type result struct { + name string + h histogram +} +type results []result + +type histogram []letter + type letter struct { r rune count int @@ -41,30 +49,6 @@ func (l letter) String() string { return fmt.Sprintf("%v: %d", string(l.r), l.count) } -type result struct { - name string - h histogram -} - -type results []result - -func (r results) Len() int { return len(r) } -func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] } -func (r results) Less(i, j int) bool { - if len(r[i].h) == 0 || len(r[j].h) == 0 { - return false - } - return r[i].h[0].count > r[j].h[0].count -} - -type histogram []letter - -func (n histogram) Len() int { return len(n) } -func (n histogram) Swap(i, j int) { n[i], n[j] = n[j], n[i] } -func (n histogram) Less(i, j int) bool { - return n[i].count > n[j].count -} - func freq(in string) histogram { m := map[rune]int{} for _, r := range in { diff --git a/sort.go b/sort.go new file mode 100644 index 0000000..c5018c5 --- /dev/null +++ b/sort.go @@ -0,0 +1,16 @@ +package main + +func (r results) Len() int { return len(r) } +func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r results) Less(i, j int) bool { + if len(r[i].h) == 0 || len(r[j].h) == 0 { + return false + } + return r[i].h[0].count > r[j].h[0].count +} + +func (n histogram) Len() int { return len(n) } +func (n histogram) Swap(i, j int) { n[i], n[j] = n[j], n[i] } +func (n histogram) Less(i, j int) bool { + return n[i].count > n[j].count +}