Domanda

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());
È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top