Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting list of list of bytes or list of byte arrays

LINQ has great OrderBy functionality with ThenBy and so on, but how could I make this work on List<List<byte>> to sort by 1st column, then by 2nd and so on.

List of list of bytes:

[0] = {0, 1, 2, 3, 4}
[1] = {0, 0, 2, 4, 1}
[2] = {1, 2, 2, 1, 1}
[3] = {1, 0, 2, 2, 2}

Actually I did same thing simply when I made string[] , but converting byte to string and then back was messy and the results where different for some reason.

I want to get:

[0] = {0, 0, 2, 4, 1}
[1] = {0, 1, 2, 3, 4}
[2] = {1, 0, 2, 2, 2}
[3] = {1, 2, 2, 1, 1}

Is it possible to use some LINQ or any other already made library to do that or maybe any suggestions how to make it manually?

like image 254
GrandaS Avatar asked May 24 '15 10:05

GrandaS


People also ask

What is byte and byte array in python?

python3's bytes and bytearray classes both hold arrays of bytes, where each byte can take on a value between 0 and 255. The primary difference is that a bytes object is immutable, meaning that once created, you cannot modify its elements. By contrast, a bytearray object allows you to modify its elements.

What is the difference between bytes and bytearray?

The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

How to define byte array in python?

Python | bytearray() functionbytearray() method returns a bytearray object which is an array of given bytes. It gives a mutable sequence of integers in the range 0 <= x < 256. Returns: Returns an array of bytes of the given size. source parameter can be used to initialize the array in few different ways.

Is bytearray mutable in python?

bytearray() Syntax bytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be modified) sequence of integers in the range 0 <= x < 256 . If you want the immutable version, use the bytes() method.


2 Answers

You could start by implementing an IComparer<IList<byte>>. E.g. (omitting null handling for brevity):

public class ByteListComparer : IComparer<IList<byte>>
{
    public int Compare(IList<byte> x, IList<byte> y)
    {
        int result;
        for(int index = 0; index<Min(x.Count, y.Count); index++)
        {
            result = x[index].CompareTo(y[index]);
            if (result != 0) return result;
        }
        return x.Count.CompareTo(y.Count);
    }
}

The above is untested (not even compiled), but should be enough to get you started.

You can then use OrderBy on your main list, passing in an instance of this comparer:

input.OrderBy(x => x, new ByteListComparer())
like image 173
Joe Avatar answered Sep 27 '22 22:09

Joe


By the way, in marked answer there is such line

for(int index = 0; index < Math.Min(x.Count, y.Count); index++)

so, function

Math.Min(x.Count, y.Count)

will be called as many times as iteration lasts.

Must be

int min=Math.Min(x.Count, y.Count);
for(int index = 0; index < min; index++)
like image 42
hhblaze Avatar answered Sep 28 '22 00:09

hhblaze