26

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 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[]'"

1
  • What are you doing next with the sortedCopy? Maybe you don't need an array. Commented Sep 13, 2012 at 11:06

3 Answers 3

58

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.

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much mr Jon! :) What does "(i => i)" mean?
@user1635406: i => i is a lambda function; it describes how to sort the array. The first i means that it takes one argument named i (you can pick any legal name); this argument is an int because that's what the array contains. The second part is the quantity that you want to sort by. In this case we want to sort each number "as itself", so i => i. The x => x.Name in Marc's answer would mean "given each element x, sort by x.Name".
won't it process faster if you first do to array and then order by?
@NevilleNazerane: No. Why would it? Also, intuition is a bad advisor where performance is concerned, but If anything, my intuition is that ToArray() first would be slower.
ok i was under the impression that the myArray variable was a list. should have noticed it's name
28

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:

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.

1 Comment

Thank you, but this time I needed a copy. Intresting reading though. :)
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.