- I wonder what is the best C# data structure I should use to sort efficiently?
- Is it List or Array or what?
- And why the standard array [] does not implement sort method in it?
Thanks
Thanks
Use a generic list, it has a built in Sort
If you are looking for an answer on which is better in your situation then my suggestion would be to run some benchmark testing against sample data you would be expecting to sort....then make a decision.
Both Lists and Arrays can be sorted, as can other data structures.
The efficiency of the sort depends on what data you are sorting, and what sorting algorithm you are using.
This site gives a good demonstration of different sorting algorithms (click on the green arrrows to start the demonstration).
The default implementation is quick sort for containers like List<>. Quick sort is about O(n * Ln(n)). For most cases it's a good choice. It is a little bit slower on small amounts of data then O(n * n) algorithms and sometimes not so good on special types of data where you'd better use special sort algorithms (suppose you can implement them by yourself or use 3-d party framework)
Actually, If list changes a lot, perhaps you should keep it ordered with a SortedList.
If sorting happens only once. And then, the reallity: if you are sorting in-memory, any O(n * Ln(n)) will suffice. You will notice no difference between List or Array or whatever.