C# 使用 orderby 对 int 数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12404735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:08:39  来源:igfitidea点击:

Sort an int array with orderby

c#

提问by user1635406

I would like to sort my int array in ascending order.

我想按升序对我的 int 数组进行排序。

first I make a copy of my array:

首先我复制我的数组:

int[] copyArray = myArray.ToArray();

Then I would like to sort it in ascending order like this:

然后我想像这样按升序对其进行排序:

 int[] sortedCopy = from element in copyArray 
                    orderby element ascending select element;

But I get a error, "selected" gets highligted and the error is: "cannot implicitly convert type 'system.linq.iorderedenumerable' to 'int[]'"

但是我收到一个错误,“selected”被高亮显示,错误是:“不能将类型‘system.linq.iorderedenumerable’隐式转换为‘int[]’”

采纳答案by Jon

You need to call ToArray()at the end to actually convert the ordered sequence into an array. LINQ uses lazy evaluation, which means that until you call ToArray(), ToList()or some other similar method the intermediate processing (in this case sorting) will not be performed.

您需要ToArray()在最后调用才能将有序序列实际转换为数组。LINQ使用懒惰评价,即,直到调用该装置ToArray()ToList()或者一些其他类似的方法在中间处理(在这种情况下分选)将不被执行。

Doing this will already make a copy of the elements, so you don't actually need to create your own copy first.

这样做已经制作了元素的副本,因此您实际上不需要先创建自己的副本。

Example:

例子:

int[] sortedCopy = (from element in myArray orderby element ascending select element)
                   .ToArray();

It would perhaps be preferable to write this in expression syntax:

用表达式语法写这个可能更可取:

int[] sortedCopy = myArray.OrderBy(i => i).ToArray();

回答by weston

We don't know what you are doing next, but maybe you don't need an array. If it's going into another linq statement, or foreach, then just keep it as it is, most simply by using var.

我们不知道您接下来要做什么,但也许您不需要数组。如果它进入另一个 linq 语句或 foreach,则保持原样,最简单的方法是使用var.

var sortedCopy = myArray.OrderBy(i => i);

foreach(var item in sortedCopy)
{
   //print out for example
}

This allows linq to be as lazy as possible. If you always cast ToArrayor ToListthen it has no choice than to evaluate then and there, and allocate memory for the result.

这允许 linq 尽可能地懒惰。如果你总是强制转换ToArrayToList那么它别无选择,只能在当时和那里评估,并为结果分配内存。

回答by Marc Gravell

Note: if you don't need a copy (i.e. it is acceptable to change myArray), then a much simpler and more efficient approach is just:

注意:如果您不需要副本(即可以接受更改myArray),那么更简单、更有效的方法就是:

Array.Sort(myArray);

This does an in-place sort of the array, exploiting the fact that it is an array to be as efficient as possible.

这对数组进行了就地排序,利用它是一个尽可能高效的数组这一事实。

For more complex scenarios (for example, a member-wise sort of an object-array), you can do things like:

对于更复杂的场景(例如,对象数组的成员类型),您可以执行以下操作:

Array.Sort(entityArray, (x,y) => string.Compare(x.Name, y.Name));

this is the moral-equivalent of:

这是道德等价物:

var sortedCopy = entityArray.OrderBy(x => x.Name).ToArray();

but again: doing the sort in-place.

但同样:就地进行排序。