Reputation: 3
So I came across over this problem which I thought is quite easy but got me thinking. The task is to sort an Ilist of numbers in ascending order. As far as I understood we can't use Sort() method for Ilists, since it is not build in the intreface. Could you please help me what would be the best and simple solution to sort an Ilist?
IList<int> list = new List<int>() { -5, 8, -7, 0, 44, 121, -7 };
Upvotes: 0
Views: 154
Reputation: 1489
if you know the unterlying type of your IList you can cast to List and use Sort method.
((List<int)list).Sort()
Upvotes: 0
Reputation: 109567
If you want to do an in-place sort, you can use ArrayList.Adapter()
As per the documentation:
The ArrayList class provides generic Reverse, BinarySearch and Sort methods. This wrapper can be a means to use those methods on IList; however, performing these generic operations through the wrapper might be less efficient than operations applied directly on the IList.
Upvotes: 0
Reputation: 23797
You can simply use Linq for the task:
var list = new List<int>() { -5, 8, -7, 0, 44, 121, -7 };
var sorted = list.OrderBy(x => x);
Upvotes: 2