Skip to content

Commit

Permalink
add a utility method for sorting CID slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Aug 31, 2018
1 parent 0e1a8db commit 0563979
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 0563979

Please sign in to comment.