1
0

move sort func into separate file

This commit is contained in:
Stephen McQuay 2016-11-06 11:17:29 -08:00
parent d61956e0fa
commit 6e3a40dd04
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
2 changed files with 24 additions and 24 deletions

32
main.go
View File

@ -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 {

16
sort.go Normal file
View File

@ -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
}