-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ipfs/fix/cid-slice
add a utility method for sorting CID slices
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |