在C#中排序更大的2d数组

时间:2022-10-14 21:41:37

I try to order this 2d-array [10000, 150] with LINQ. I want to order it dependent of the second column. And I can't use a one-dimensional array.

我尝试用LINQ命令这个2d数组[10000,150]。我想订购它依赖于第二列。我不能使用一维数组。

namespace test2 {
    class Test {
        public static void Main() {
            string[,] array = new string[,]
            {
                {"cat", "dog"},
                {"bird", "fish"},
            };
            array = array.OrderBy (a => a [1]);
        }
    }
}

But I get the error: /home/Program.cs(18,18): Error CS0411: The type arguments for method `System.Linq.Enumerable.OrderBy(this System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly (CS0411) (test2)

但我收到错误:/home/Program.cs(18,18):错误CS0411:方法`System.Linq.Enumerable.OrderBy(此System.Collections.Generic.IEnumerable,System.Func)'的类型参数不能从用法推断。尝试明确指定类型参数(CS0411)(test2)

How can I specify the type arguments?

如何指定类型参数?

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


LINQ functions does not work on multidimensional arrays. You could just convert it to this:

LINQ函数不适用于多维数组。你可以把它转换成这个:

string[][] array = new string[][]
{
    new [] {"cat", "dog"},
    new [] {"bird", "fish"},
};

var result = array.OrderBy(a => a[1]);

#1


LINQ functions does not work on multidimensional arrays. You could just convert it to this:

LINQ函数不适用于多维数组。你可以把它转换成这个:

string[][] array = new string[][]
{
    new [] {"cat", "dog"},
    new [] {"bird", "fish"},
};

var result = array.OrderBy(a => a[1]);