From 98d5dd7033b7947c9ce4cca9f3ffb950e1ee5e7c Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (smcquay)" Date: Wed, 18 Oct 2017 17:43:26 -0700 Subject: [PATCH] Sort output before it goes out Fixes #1 --- hash.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hash.go b/hash.go index 01d2261..30a8472 100644 --- a/hash.go +++ b/hash.go @@ -9,6 +9,7 @@ import ( "hash" "io" "os" + "sort" "sync" ) @@ -19,6 +20,13 @@ type result struct { err error } +// results exists to sort a slice of result +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 { return r[i].f < r[j].f } + // hashr exists so that we can make a thing that can return valid hash.Hash // interfaces. type hashr func() hash.Hash @@ -72,7 +80,19 @@ func hsh(files []string) chan result { res = append(res, compute(h, jobs)) } - return rmerge(res) + o := make(chan result) + go func() { + rs := results{} + for r := range rmerge(res) { + rs = append(rs, r) + } + sort.Sort(rs) + for _, r := range rs { + o <- r + } + close(o) + }() + return o } // compute is the checksumming workhorse