[Solved] why arrayList.sort() doestnt work with me?


You are adding two byte arrays in your ArrayList and trying to sort them. Since sorting is performed on a comparison basis… at runtime your application complains about a missing criterion for defining a priority between the two arrays.

public class BytesComparer : IComparer
{
    Int32 IComparer.Compare(Object x, Object y)
    {
        Byte[] left = (Byte[])x;
        Int32 val1 = BitConverter.ToInt32(left.Take(4).ToArray(), 0);

        Byte[] right = (Byte[])y;
        Int32 val2 = BitConverter.ToInt32(right.Take(4).ToArray(), 0);

        return val1.CompareTo(val2);
    }
}

public static void Main(string[] args)
{
    ArrayList x = new ArrayList();
    Byte[] sevenItems1 = new Byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
    Byte[] sevenItems2 = new Byte[] { 0x10, 0x00, 0x20, 0x20, 0x20, 0x20,0x20 };

    x.Add(sevenItems1);
    x.Add(sevenItems2); 
    x.Sort(new BytesComparer());
}

0

solved why arrayList.sort() doestnt work with me?