views:

128

answers:

5
  • 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

+1  A: 

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.

James
I know, but I am asking what is more efficient List sorting or Array
Betamoo
@betamoo: as already suggested it is based on the data you are sorting. If you want to test the effeciency of both then you are best doing some benchmarking with both approaches
James
@James I think that List expandable manner should decrease its sort efficiency.. of course it is based on its implementation in c#
Betamoo
+2  A: 

Sorting efficiency depends on the amount of data and the sort rules. There is no one definite answer to your question.

The array[] class does implement sort. See here. It is a static method to be called as Array.Sort.

Oded
sorting 32bit integers for exapmle that can fit in memory
Betamoo
How many? 10? 1000? 100000? On what hardware? Using parallel extensions? You need to _test_ things yourself to find out.
Oded
+2  A: 

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).

Colin Pickard
+1 - neat site.
ScottE
A: 

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)

Voice
A: 

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.

Daniel Dolz