2
gridMessages.OrderBy(x => Convert.ToInt32(x.age));

So age, is a string that could be something like "0", or "14", you get the picture. I wanted to sort the list by the age, so I convert it to an int in the OrderBy. The code compiles and runs without error but the list is not sorted.

What am I doing wrong?

2 Answers 2

6

You're not using the results. OrderBy returns a sorted list!

gridMessages = gridMessages.OrderBy(x => Convert.ToInt32(x.age));
Sign up to request clarification or add additional context in comments.

1 Comment

Not at all, a very common gotcha
0

If gridMessages is a List<T> you can also sort it in place instead of creating a new list:

gridMessages.Sort( (a,b) => a.Age.CompareTo(b.Age));

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.