Skip to content

Commit

Permalink
Merge pull request #5 from ipfs/fix/cid-slice
Browse files Browse the repository at this point in the history
add a utility method for sorting CID slices
  • Loading branch information
Stebalien authored Sep 24, 2018
2 parents 6c3d0c5 + e23b860 commit 60d67ce
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 60d67ce

Please sign in to comment.