Skip to content

Commit 0563979

Browse files
committed
add a utility method for sorting CID slices
fixes ipfs/go-cid#46
1 parent 0e1a8db commit 0563979

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

slice.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cidutil
2+
3+
import (
4+
"github.com/ipfs/go-cid"
5+
"sort"
6+
)
7+
8+
// Slice is a convenience type for sorting CIDs
9+
type Slice []cid.Cid
10+
11+
func (s Slice) Len() int {
12+
return len(s)
13+
}
14+
15+
func (s Slice) Less(i, j int) bool {
16+
return s[i].KeyString() < s[j].KeyString()
17+
}
18+
19+
func (s Slice) Swap(i, j int) {
20+
s[i], s[j] = s[j], s[i]
21+
}
22+
23+
func (s Slice) Sort() {
24+
sort.Sort(s)
25+
}
26+
27+
// Sort sorts a slice of CIDs
28+
func Sort(s []cid.Cid) {
29+
Slice(s).Sort()
30+
}

0 commit comments

Comments
 (0)