C# 排序列表<;字节[]>;从特定的listindex到c中列表的末尾#

C# 排序列表<;字节[]>;从特定的listindex到c中列表的末尾#,c#,list,sorting,C#,List,Sorting,几天来我一直在尝试这样做,但我所能做的就是对完整的列表进行排序,但无法从特定的索引进行排序 比如说,我有下面的列表 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

几天来我一直在尝试这样做,但我所能做的就是对完整的列表进行排序,但无法从特定的索引进行排序

比如说,我有下面的列表

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排序到列表的末尾

结果列表应如下所示:

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

这只是一个示例。但实际列表可以包含N个字节[]。

您可以使用以下扩展方法

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中的每个数组进行排序,则可以执行以下操作:

var start = 2;
foreach (var entry in byteArrayList)
{
    entry.PartialSort(start, entry.Length - 1);
}
var ListPosition=2;
for (var index = 0; index < byteArrayList.Count; index++)
{
    if (index >= ListPosition)
        byteArrayList[index].PartialSort(0, byteArrayList[index].Length - 1);
}
var ListPosition=2;
对于(var index=0;index=ListPosition)
byteArrayList[index].PartialSort(0,byteArrayList[index].Length-1);
}
这是一张工作票

已更新

根据您的注释,您只想对列表进行排序,而不想对其中的数组进行排序,因此您可以执行以下操作:

  • 定义自定义比较器
  • 使用列表的排序方法
  • 比较器类

    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;
        }
    }
    
    公共类ByteArrayComparer:IComparer
    {
    公共整数比较(字节[]第一,字节[]第二)
    {
    //找到两个数组的最小长度
    var length=first.length>second.length?second.length:first.length;
    对于(var索引=0;索引<长度;索引++)
    {
    如果(第一个[索引]>第二个[索引])
    返回1;
    if(第二个[索引]>第一个[索引])
    返回-1;
    }
    返回0;
    }
    }
    
    您的代码应该如下所示

    var ListPosition = 2;
    if(ListPosition< byteArrayList.Count && ListPosition>-1)
         byteArrayList.Sort(start,byteArrayList.Count - start, new ByteArrayComparer());
    
    var ListPosition=2;
    if(ListPosition-1)
    排序(start,byteArrayList.Count-start,newbytearraycomparer());
    
    这里有一个工作

    试试这个

    arrayList.Select(x => x.Take(2).Concat(x.Skip(2).OrderBy(y => y)).ToArray()).ToList();
    
    如果我们再看一遍

    • 选择第一个字节数组并获取前2个索引。(因为我们会
      concat到排序列表)
    • 然后在
      Concat
      中,我们
      跳过前2个索引并对其排序
    • 在这些之后,前两个索引和排序后的rest相互绑定
    (并返回剩余的元素,这就是使用的原因。)

    这是你的电话号码


    希望有帮助,

    这是自定义比较器:

    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);
        }
    }
    
    公共类CompareByteArrays:IComparer
    {
    公共整数比较(字节[]a1,字节[]a2)
    {
    int shorterLength=a1.长度a2[i])
    {
    返回1;
    }
    }
    返回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();
    }
    
    公共列表SortFromIndex(列表源,int索引)
    {
    返回source.Take(index).Concat(source.Skip(index).OrderBy(o=>o,newcomparebyterarrays()).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);
    
    List byteArrayList=new List();
    Add(新字节[]{1,2,3,5,9,6,7,6,45,50,39});
    Add(新字节[]{0,1,0,1,0,1,1,0,1,99,99,99,99});
    Add(新字节[]{2,2,2,3,3,3});
    Add(新字节[]{0,0,0,0,0,0,0,0,0,31,21});
    Add(新字节[]{1,22,32,22,3,3,3,3,12,13,14,15});
    Add(新字节[]{0,0,0,0,0,0,0,0,0,95,85,75});
    列表结果=SortFromIndex(byteArrayList,2);
    
    您可以利用LINQ:
    List.Take(2).Concat(List.Skip(2).OrderBy(…)
    。您需要将一个自定义的
    比较器
    传递给
    OrderBy
    ,以便以您想要的方式比较两个列表。您使用什么代码进行排序?如果没有这些信息,这不是一个好问题。您正在对列表中每个数组的每个元素进行排序。我正试着把清单分类。(从位置2)即:从byteArrayList[2]到byteArrayList[endOfList]。(不排序每个字节[]的元素)。请通过tearraylist检查我的结果。为了澄清,如果排序索引大于列表中数组的上一个,结果是什么?它将被考虑在排序的顶部?如果索引=2上的3个数组具有相同的值,让我们说3,哪个数组将首先出现?有什么标准吗?例如检查下一个索引?1)如果(listPosition>byteArrayList.Count)//什么也不做2)如果3个字节[]具有相同的值。那么没有具体的标准。只是一个接一个地排序。我并没有试图从位置2对字节[]的每个元素进行排序。但我尝试的是从ListPosition==2对列表进行排序。ie:从byteArrayList[2]到byteArrayList[endOfList](不排序字节[]的每个元素)
    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);