From e14088c88180ad5ee69b10722a54baddd3908512 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 31 Aug 2018 09:23:10 -0700 Subject: [PATCH] add a utility method for sorting CID slices fixes #46 --- slice.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 slice.go diff --git a/slice.go b/slice.go new file mode 100644 index 0000000..fa39b64 --- /dev/null +++ b/slice.go @@ -0,0 +1,30 @@ +package cidutil + +import ( + "github.com/ipfs/go-cid" + "sort" +) + +// Slice is a convenience type for sorting CIDs +type Slice []cid.Cid + +func (s Slice) Len() int { + return len(s) +} + +func (s Slice) Less(i, j int) bool { + return s[i].KeyString() < s[j].KeyString() +} + +func (s Slice) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func (s Slice) Sort() { + sort.Sort(s) +} + +// Sort sorts a slice of CIDs +func Sort(s []cid.Cid) { + Slice(s).Sort() +}