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 };
CodePudding user response:
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);
CodePudding user response:
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.
CodePudding user response:
if you know the unterlying type of your IList you can cast to List and use Sort method.
((List<int)list).Sort()