Sort an int array with orderby

后端 未结 3 1442
-上瘾入骨i
-上瘾入骨i 2020-12-16 10:49

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

first I make a copy of my array:

int[] copyArray = myArray.ToArray();

Then I

3条回答
  •  被撕碎了的回忆
    2020-12-16 11:17

    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.

    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 ToArray or ToList then it has no choice than to evaluate then and there, and allocate memory for the result.

提交回复
热议问题