C# Linq OrderBy(字节[])值

C# Linq OrderBy(字节[])值,c#,linq,C#,Linq,如何按字节[]值排序?您不能直接按字节[]排序,因为数组不实现IComparable。您需要按照第一个字节排序(即:OrderBy(f=>f.sort[0])或其他适当的方式),或者编写自己的IComparer并将其用于适当的重载。不幸的是,据我所知,您无法按照字节数组排序 您可以做的是让您的foo类实现IComparable。然后在overrideen compareTo方法中,根据您在调用中的喜好编写字节数组的比较。然后,您可以通过简单的排序来回复订单: System.ArgumentExc

如何按字节[]值排序?

您不能直接按
字节[]
排序,因为数组不实现
IComparable
。您需要按照第一个字节排序(即:
OrderBy(f=>f.sort[0])
或其他适当的方式),或者编写自己的
IComparer
并将其用于适当的重载。

不幸的是,据我所知,您无法按照字节数组排序

您可以做的是让您的foo类实现IComparable。然后在overrideen compareTo方法中,根据您在调用中的喜好编写字节数组的比较。然后,您可以通过简单的排序来回复订单:

System.ArgumentException: At least one object must implement IComparable.
作为一个SQL Server层次结构ID,您绝对需要创建一个自定义实现

逻辑很简单:

  • 比较每个数组字节的第一个
    n
    字节,其中
    n
    是两个数组中较小的字节数。当检测到任何字节之间存在差异时,返回不同字节的比较结果
  • 如果第一个
    n
    字节相等,则返回两个数组长度的比较
这样,给定一组如下数据:

FooSource().Sort();   
排序后,您将得到以下结果:

00 01 02
00 01
01
也就是说,这就是您的
IComparer
实现的样子:

00 01
00 01 02
01
//我可能错了,因为这叫做自然顺序。
类NaturalOrderByteArrayComparer:IComparer
{
公共整数比较(字节[]x,字节[]y)
{
//快捷方式:如果两者都为空,则它们是相同的。
如果(x==null&&y==null)返回0;
//如果一个为null,另一个为null,则
//一个为空的是“较小的”。
如果(x==null&&y!=null)返回-1;
如果(x!=null&&y==null)返回1;
//两个数组都不为空。请查找较短的数组
//两种长度中的一种。
int bytesToCompare=Math.Min(x.Length,y.Length);
//比较字节。
for(int index=0;index
另一方面,如果保证字节数组的长度相同,您可以动态创建order by子句,按第一个元素排序,然后按第二个元素排序,等等,如下所示:

// I could be wrong in that this is called natural order.
class NaturalOrderByteArrayComparer : IComparer<byte[]>
{
    public int Compare(byte[] x, byte[] y)
    {
        // Shortcuts: If both are null, they are the same.
        if (x == null && y == null) return 0;

        // If one is null and the other isn't, then the
        // one that is null is "lesser".
        if (x == null && y != null) return -1;
        if (x != null && y == null) return 1;

        // Both arrays are non-null.  Find the shorter
        // of the two lengths.
        int bytesToCompare = Math.Min(x.Length, y.Length);

        // Compare the bytes.
        for (int index = 0; index < bytesToCompare; ++index)
        {
            // The x and y bytes.
            byte xByte = x[index];
            byte yByte = y[index];

            // Compare result.
            int compareResult = Comparer<byte>.Default.Compare(xByte, yByte);

            // If not the same, then return the result of the
            // comparison of the bytes, as they were the same
            // up until now.
            if (compareResult != 0) return compareResult;

            // They are the same, continue.
        }

        // The first n bytes are the same.  Compare lengths.
        // If the lengths are the same, the arrays
        // are the same.
        if (x.Length == y.Length) return 0;

        // Compare lengths.
        return x.Length < y.Length ? -1 : 1;
    }
}
静态IEnumerable OrderBySortField(此IEnumerable项,
整数排序长度)
{
//验证参数。
如果(items==null)抛出新的ArgumentNullException(“items”);
如果(sortLength<0)抛出
新ArgumentOutOfRangeException(“sortLength”,sortLength,
“sortLength参数必须是非负值。”);
//快捷方式,如果sortLength为零,则按原样返回序列。
如果(sortLength==0)返回项目;
//有序枚举。
IOrderedEnumerable ordered=items.OrderBy(i=>i.sort[0]);
//从第二个索引开始循环。
for(int index=1;indexi.sort[indexCopy]);
}
//返回有序的可枚举项。
订购退货;
}
然后你可以简单地这样称呼它:

static IEnumerable<foo> OrderBySortField(this IEnumerable<foo> items, 
    int sortLength)
{
    // Validate parameters.
    if (items == null) throw new ArgumentNullException("items");
    if (sortLength < 0) throw 
        new ArgumentOutOfRangeException("sortLength", sortLength,
            "The sortLength parameter must be a non-negative value.");

    // Shortcut, if sortLength is zero, return the sequence, as-is.
    if (sortLength == 0) return items;

    // The ordered enumerable.
    IOrderedEnumerable<foo> ordered = items.OrderBy(i => i.sort[0]);

    // Cycle from the second index on.
    for (int index = 1; index < sortLength; index++)
    {
        // Copy the index.
        int indexCopy = index;

        // Sort by the next item in the array.
        ordered = ordered.ThenBy(i => i.sort[indexCopy]);
    }

    // Return the ordered enumerable.
    return ordered;
}
//您必须提供正在排序的数组的长度。
List sortedFoo=FooSource()。
OrderBySortField(sortLength).ToList();

我知道,这是一个老问题,但在特定情况下,当字节数组包含一个数字(例如IP地址)时,BitConverter类可用:

// You have to supply the length of the array you're sorting on.
List<foo> sortedFoo = FooSource().
    OrderBySortField(sortLength).ToList();

来源:

您必须实现接口IComparable并为订单创建您自己的函数。因此,如果
f1
f1.sort=={3,201,25,}
f2
f2.sort=={3,29,7,222,0,}
哪个更大,
f1
f2
,为什么?出于我的目的,f1将被视为大于f2,因为201>29。我可以理解为什么从“正确的行为”的角度将这种行为烘焙到框架中会很困难。数组的长度不是固定的,因为它是SQL server层次结构的表示形式。@StormRider01我已更新了答案以解决您的特定实例。我已经留下了以前的解决方案,但是首先显示的是处理可变长度数组的解决方案。
// You have to supply the length of the array you're sorting on.
List<foo> sortedFoo = FooSource().
    OrderBySortField(sortLength).ToList();
OrderBy(d => BitConverter.ToInt32(d.bytearray,0))