简体   繁体   English

C#排序问题

[英]C# Sorting Question

  • I wonder what is the best C# data structure I should use to sort efficiently? 我想知道什么是我应该用来有效排序的最好的C#数据结构?
  • Is it List or Array or what? 是列表还是数组还是什么?
  • And why the standard array [] does not implement sort method in it? 为什么标准数组[]没有实现排序方法呢?

Thanks 谢谢

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. array []类确实实现了排序。 See here . 看到这里 It is a static method to be called as Array.Sort . 它是一个被称为Array.Sort的静态方法。

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). 该站点很好地演示了不同的排序算法(点击绿色箭头开始演示)。

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. 如果您正在寻找一个在您的情况下更好的答案,那么我的建议是针对您期望排序的样本数据运行一些基准测试....然后做出决定。

The default implementation is quick sort for containers like List<>. 默认实现是对List <>之类的容器进行快速排序。 Quick sort is about O(n * Ln(n)). 快速排序约为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) 对于少量数据然后O(n * n)算法有点慢,有时在特殊类型的数据上不太好,你最好使用特殊的排序算法(假设你可以自己实现或使用3- d党框架)

Actually, If list changes a lot, perhaps you should keep it ordered with a SortedList. 实际上,如果列表发生了很大变化,也许您应该使用SortedList对其进行排序。

If sorting happens only once. 如果排序只发生一次。 And then, the reallity: if you are sorting in-memory, any O(n * Ln(n)) will suffice. 然后,真实性:如果你在内存中排序,任何O(n * Ln(n))就足够了。 You will notice no difference between List or Array or whatever. 您会注意到List或Array之间没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM