【发布时间】:2019-06-08 17:32:15
【问题描述】:
我有一个二维数组,其中包含 6 位不同销售人员的已售商品数量。我正在尝试对数组进行排序以找到所有商品中销量最高的商品。
我的问题是能够对二维数组进行排序并读取其内容。
我试过了
int[,] sortedByFirstElement = nrArticles.OrderBy(x => x[0]);
这给了我一条错误消息:“'int[,] 不包含 'OrderBy' 的定义并且没有可访问的扩展方法......等等”
我已经尝试过,它给了我一条错误消息,声称输入不在 int 中。
Array.Sort(nrArticles);
foreach (int value in nrArticles)
{
Console.Write(value);
}
以下代码是我到目前为止所得到的,不包括排序尝试。
string[,] salesMan = new string[6, 3];
int[,] nrArticles = new int[6, 0];
string line;
int nr;
for (int i = 0; i < 5; i++)
{
Console.Write("Enter the name of salesman: ");
line = Console.ReadLine();
salesMan[i, 0] = line;
Console.Write("Enter the social number: ");
line = Console.ReadLine();
salesMan[i, 1] = line;
Console.Write("Enter the district: ");
line = Console.ReadLine();
salesMan[i, 2] = line;
//This is where i convert the entered value into an int taken
by the array
Console.Write("Enter the amount of sold articles: ");
line = Console.ReadLine();
nr = Int32.Parse(line);
nrArticles[i, 0] = nr;
}
Console.Write("Namn \t\tPersnr \t\t\tDistrikt \t\tAntal");
for (int j = 0; j < 5; j++)
{
Console.Write("\n"+salesMan[j, 0] +"\t\t");
Console.Write(salesMan[j, 1] + "\t\t\t");
Console.Write(salesMan[j, 2] + "\t\t\t");
Console.Write(nrArticles[j, 0]);
}
//Here's where i'd put my code of sorting my integer array
nrArticles (If i had one).
}
}
它的预期结果会是这样的例子:231、183、130、122、40、10。也许是一个单独的数组,甚至是字符串?
任何帮助将不胜感激。
【问题讨论】:
-
这是一个很好的问题,但我很好奇你为什么不使用
Dictionary<int, List<int>>来代替?你可以使用LINQ。
标签: c# arrays sorting multidimensional-array