如何使用 Linq 对二维数组进行排序?
我有一个二维字符串数组
string [] [] myArray;
,我想按其中一列对其进行排序。
所以数据可能是
{ "Apple", "2", "Bob" },
{ "Banana", "1", "Fred" }
,我想按这些列中的任何一个对它进行排序 - 我将有一个索引。
理想情况下,我想做类似 myArray.Sort(1);
我知道我可能必须使用自定义比较器。我认为这是一个有趣的学习机会。有人可以提供一些建议吗?
I have a 2d array of strings
string [] [] myArray;
and I want to sort it by one of the columns.
So the data might be
{ "Apple", "2", "Bob" },
{ "Banana", "1", "Fred" }
And I want to sort it by any of those columns - I'll have an index.
Ideally, I'd like to do something like myArray.Sort(1);
I understand I may have to use a custom comparer. I see this as an interesting learning opportunity. Can anyone offer some advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将对数组进行排序(因此最后
myArray
将被排序)。相比之下,LINQOrderBy
创建一个新的有序枚举,然后您可以将其转换为ToArray
。This will order the array in place (so at the end
myArray
will be sorted). LINQOrderBy
by comparison creates a new ordered enumerable that then you can convert to aToArray
.你可以这样做......
然后写例如:
you can do like this...
And then write for example: