2016-09-28 59 views
2

我一直试图做这从几天,但我可以做的是排序完整列表,但无法从特定索引进行排序。排序列表<byte[]>从特定列表索引直到列表的末尾在c#

可以说我有例如

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}); 

下面的列表中,并让说,当前的列表索引

ListPoisition = 2; 

因此清单应当由ListPoisition == 2进行排序,直到的结束名单。

结果列表看起来应该像:

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 } 
       }; 

这只是一个example.But实际列表可以包含的byte []的N多。这里

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

工作demo

+0

你可以利用LINQ:'List.Take(2).Concat(名单。跳过(2).OrderBy(...))'。你需要传递一个自定义的'Comparer'到'OrderBy'来按照你想要的方式比较两个列表。 –

+0

如果你有什么东西可以工作,即使它不完美,发布它也不会有什么伤害。 –

+0

你用什么代码进行排序?没有这些信息,这不是一个好问题。 – Phil1970

回答

0

这是自定义比较:

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); 
    } 
} 

然后这是该函数将其从某些指标进行排序:

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

因此,这是你如何在主拨打:

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); 
+0

完美代码。正如我所需要的那样工作。 –

1

您可以使用下面的扩展方法

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]; 
} 

如果你想从位置2列表中的每个数组进行排序,那么你可以做以下

如果要从列表中的索引2对每个数组进行排序,则可以执行以下操作:

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

这里工作demo

修订

根据您的意见,您要在列表进行排序,而不是在阵列内,所以在这里你可以做什么:

  1. 定义自定义比较器
  2. 使用列表的排序方法

的比较器类

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; 
    } 
} 

你的代码看起来应该是这样

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

这里工作demo

+0

您正在排序列表中每个数组的每个元素。我正在尝试对列表进行排序。 (从位置2开始)即:从byteArrayList [2]到byteArrayList [endOfList]。 (不对每个字节[]的元素进行排序)。请检查我的结果byteArrayList。 –

+0

澄清,如果排序的索引大于列表中的数组的结果,结果如何?它会被认为是最重要的? – Monah

+0

如果index = 2上的3个数组具有相同的值,那么让我们说3,哪个数组将首先出现?有什么标准吗?例如检查下一个索引? – Monah

0

试试这个,

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

如果我们检查一下;

  • Select第一个字节的数组并取前两个索引。(因为我们将 CONCAT到排序列表)
  • 然后在Concat,我们skip第2指标,并责令,
  • 这些之后,前两个指数和分类休息绑定到对方。

Skip和剩余的元素Take的回报,这就是为什么使用Concat。)

这里是result

希望帮助,

+0

我不想从位置2排序字节[]的每个元素。但我所尝试的是从ListPosition == 2排序列表。ie:from byteArrayList [2]直到byteArrayList [endOfList](没有排序字节[]的每个元素) –