2

I want to compare byte arrays used as keys in a SortedList

public SortedList<byte[], string> myList = new SortedList<byte[], string>();

the problem is, that I cant add entries into myList, because .NET doesnt know how to compare two elements of the list ("Error comparing two elements in array"):

byte[] bytearray = {0,0,25,125,250}; // Example 
string description = "Example Value";
myList.Add(bytearray, description);

After a bit of googling I read something about implementing my own IComparer Class. I've searched further but didn't found anything about an IComparer implementation for byte arrays. Do you have any idea how to accomplish this?

Quick Edit: Thanks for the answers! I've implemented the IComparer from the answer provided:

    class ByteComparer : IComparer<byte[]>
    {
        public int Compare(byte[] x, byte[] y)
        {
            var len = Math.Min(x.Length, y.Length);
            for (var i = 0; i < len; i++)
            {
                var c = x[i].CompareTo(y[i]);
                if (c != 0)
                {
                    return c;
                }
            }

            return x.Length.CompareTo(y.Length);
        }
    }

And calling it with:

    public SortedList<byte[], string> myList = new SortedList<byte[], string>(new ByteComparer());
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Live
  • 120
  • 9
  • Just do a Array.SequenceEquals between the values in **your own IComperer** implementation. – M.Babcock Oct 30 '13 at 23:19
  • But doesnt it return only one value that is useful for me? SequenceEquals would provide me the information if the arrays are equal, how about being "larger" or "lower" (1,0,-1 values for IComprarer) ? – Live Oct 30 '13 at 23:24

3 Answers3

4

How about something like this?

class ByteComparer : IComparer<byte[]>
{
    public int Compare(byte[] x, byte[] y)
    {
        var len = Math.Min(x.Length, y.Length);
        for (var i = 0; i < len; i++)
        {
            var c = x[i].CompareTo(y[i]);
            if (c != 0)
            {
                return c;
            }
        }

        return x.Length.CompareTo(y.Length);
    }
}

This can even be extended to a generic class for comparing arrays of any type which implements IComparable<T>:

class ArrayComparer<T> : IComparer<T[]>
    where T : IComparable<T>
{
    public int Compare(T[] x, T[] y)
    {
        var len = Math.Min(x.Length, y.Length);
        for (var i = 0; i < len; i++)
        {
            var c = x[i].CompareTo(y[i]);
            if (c != 0)
            {
                return c;
            }
        }

        return x.Length.CompareTo(y.Length);
    }
}

This will find the first element which differs between the two arrays, and return a value indicating which comes first according the types default ordering. If there are no differences, it will return a value indicating which array is shorter.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

You need to create a class that implement the IComparer<byte[]> interface. This interface has one method - public int Compare(byte[] first, byte[] second). The method should return a negative int if first < second, 0 if first == second, and a positive int if first > second. You'll need to work out what <, ==, and > mean in your application and write the method accordingly.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
0

It really depends on your logic flow. What does it mean byte array a is less than byte array b? What should you compare? Anyway, you just have to implement one simple method: int Compare(byte[] x, byte[] y)

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125