Skip to content

Commit 2cd334b

Browse files
committed
Add method: (c *Cid) Cmp(o *Cid) int
1 parent 1afa3fe commit 2cd334b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cid.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ func (c Cid) Bytes() []byte {
410410
return []byte(c.str)
411411
}
412412

413+
// Cmp compares two Cids for binary sorting order.
414+
func (c *Cid) Cmp(o *Cid) int {
415+
switch {
416+
case c == o:
417+
return 0
418+
case c == nil:
419+
return -1
420+
case o == nil:
421+
return 1
422+
default:
423+
return strings.Compare(c.str, o.str)
424+
}
425+
}
426+
413427
// ByteLen returns the length of the CID in bytes.
414428
// It's equivalent to `len(c.Bytes())`, but works without an allocation,
415429
// and should therefore be preferred.

0 commit comments

Comments
 (0)