This Question have 3 answers right now.

Sort List<byte[]> from specific listindex till the end of the list in c#

Question!

I have been trying to do this from few days, but all i could do is sort the complete list but not able to sort from a specific index.

lets say i have the following list for example

List<byte[]> byteArrayList = new list<byte[]>();
byteArrayList.Add(new byte[]{1, 2, 3, 5, 9, 6, 7, 6, 45, 50, 39 });
byteArrayList.Add(new byte[]{0, 1, 0, 1, 0, 1, 0, 1, 99, 99, 99, 99, 99, 99});
byteArrayList.Add(new byte[]{2, 2, 2, 2, 3, 3, 3, 3 });
byteArrayList.Add(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 31, 21 });
byteArrayList.Add(new byte[]{1, 22, 32, 22, 3, 3, 3, 3, 12, 13, 14, 15 });
byteArrayList.Add(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 95, 85, 75});

and lets say the current list index

ListPoisition = 2;

Therefore the list should be sorted from ListPoisition == 2 till the end of the list.

The result list should look like:

byteArrayList = {  {1, 2, 3, 5, 9, 6, 7, 6, 45, 50, 39 },
                   {0, 1, 0, 1, 0, 1, 0, 1, 99, 99, 99, 99, 99, 99 },
                   {0, 0, 0, 0, 0, 0, 0, 0, 31, 21 },
                   {0, 0, 0, 0, 0, 0, 0, 0, 95, 85, 75},
                   {1, 22, 32, 22, 3, 3, 3, 3, 12, 13, 14, 15 },
                   {2, 2, 2, 2, 3, 3, 3, 3 }
                };

This is just an example.But the actual list can contain N number of byte[].

By : Pratul Jn


Answers

You can use the following extension method

public static void PartialSort<T>(this T[] array, int startIndex, int endIndex)  
{  
    T[] sortedList = new T[(endIndex - startIndex) + 1];  

    for (int i = startIndex; i <= endIndex; i++)  
    {  
        sortedList[i - startIndex] = array[i];  
    }  
    List<T> newList = new List<T>(sortedList);  
    newList.Sort();  
    sortedList = newList.ToArray();  

    for (int i = 0; i < sortedList.Length; i++)  
        array[i + startIndex] = sortedList[i];  
}

If you want to sort each array in the list from position 2, then you can do the following:

var start = 2;
foreach (var entry in byteArrayList)
{
    entry.PartialSort(start, entry.Length - 1);
}

here a working demo

If you want to sort each array from the index 2 from the list, then you can do the following:

var ListPosition=2;
for (var index = 0; index < byteArrayList.Count; index++)
{
    if (index >= ListPosition)
        byteArrayList[index].PartialSort(0, byteArrayList[index].Length - 1);
}

Here a working demo

UPDATED

Based on your the comments, you want to sort the list only and not the array inside, so here what you can do:

  1. Define a custom comparer
  2. Use the Sort method of the List

The comparer class

public class ByteArrayComparer : IComparer<byte[]>
{
    public int Compare(byte[] first, byte[] second)
    {
        // find the minimum length of the both arrays
        var length = first.Length > second.Length ? second.Length : first.Length;
        for (var index = 0; index < length; index++)
        {
             if (first[index] > second[index])
                  return 1;
             if (second[index] > first[index])
                  return -1;
        }
        return 0;
    }
}

Your code should look like this

var ListPosition = 2;
if(ListPosition< byteArrayList.Count && ListPosition>-1)
     byteArrayList.Sort(start,byteArrayList.Count - start, new ByteArrayComparer());

Here a working demo



This is the custom comparer:

public class CompareByteArrays : IComparer<byte[]>
{
    public int Compare(byte[] a1, byte[] a2)
    {
        int shorterLength = a1.Length < a2.Length ? a1.Length : a2.Length;
        for(int i = 0; i < shorterLength; i++)
        {
            if(a1[i] < a2[i])
            {
                return -1;
            }
            else if(a1[i] > a2[i])
            {
                return 1;
            }
        }
        return a1.Length.CompareTo(a2.Length);
    }
}

And then this is the function to sort it from certain index:

public List<byte[]> SortFromIndex(List<byte[]> source, int index)
{
    return source.Take(index).Concat(source.Skip(index).OrderBy(o=>o, new CompareByteArrays())).ToList();
}

So this is how you would call in the main:

List<byte[]> byteArrayList = new List<byte[]>();
byteArrayList.Add(new byte[] { 1, 2, 3, 5, 9, 6, 7, 6, 45, 50, 39 });
byteArrayList.Add(new byte[] { 0, 1, 0, 1, 0, 1, 0, 1, 99, 99, 99, 99, 99, 99 });
byteArrayList.Add(new byte[] { 2, 2, 2, 2, 3, 3, 3, 3 });
byteArrayList.Add(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 31, 21 });
byteArrayList.Add(new byte[] { 1, 22, 32, 22, 3, 3, 3, 3, 12, 13, 14, 15 });
byteArrayList.Add(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 95, 85, 75 });

List<byte[]> result = SortFromIndex(byteArrayList, 2);


Try this,

arrayList.Select(x => x.Take(2).Concat(x.Skip(2).OrderBy(y => y)).ToArray()).ToList();

If we go over it;

  • Select first byte array and takes first 2 index. (because we will concat to sorted list)
  • Then in Concat, we skip first 2 index and order it,
  • After these, first two index and sorted rest binded to each other.

(Skipand Takereturns remaining elements, thats why Concat used.)

Here is the result

Hope helps,

By : Berkay


Video about Sort List<byte[]> from specific listindex till the end of the list in c#