diff --git a/user/id.go b/user/id.go index c8e2d14..0529414 100644 --- a/user/id.go +++ b/user/id.go @@ -123,3 +123,9 @@ func (x *ID) setUserID(w []byte) error { return nil } + +// Cmp returns an integer comparing two base58 encoded user ID lexicographically. +// The result will be 0 if a == b, -1 if a < b, and +1 if a > b. +func (x ID) Cmp(x2 ID) int { + return bytes.Compare([]byte(x.EncodeToString()), []byte(x2.EncodeToString())) +} diff --git a/user/id_test.go b/user/id_test.go index c517dc5..78d4b83 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -3,6 +3,7 @@ package user_test import ( "bytes" "crypto/rand" + "slices" "testing" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" @@ -132,3 +133,18 @@ func TestID_Equal(t *testing.T) { require.True(t, id3.Equals(id1)) // commutativity require.False(t, id1.Equals(id2)) } + +func TestID__Cmp(t *testing.T) { + id1 := usertest.ID() + id2 := usertest.ID() + id3 := usertest.ID() + + arr := []ID{id1, id2, id3} + + slices.SortFunc(arr, ID.Cmp) + for i := 1; i < len(arr); i++ { + if bytes.Compare([]byte(arr[i-1].EncodeToString()), []byte(arr[i].EncodeToString())) > 0 { + t.Fatalf("arrays are not sorted correctly") + } + } +}