在通用列表中排序 [英] Sorting in Generic List
问题描述
有以下方法对List< t>
1的集合进行排序 - 使用委托
2 - 使用lambda表达式
3 - 使用IComparer
4 - 在T类中实现IComparable
是否应该使用上述4种技术中的哪些技术有什么好处,是否有任何相关性能。
我知道如果我们在课堂上实现IComparable,它可以更好地维护和读取如果他有多个分类标准我们可以有IComparer。
感谢大家的意见。
问候,
Sakshi
Hi,
There are the following methods to sort a collection of List<t>
1-Using delegate
2-Using lambda expressions
3-Using IComparer
4-Implementing IComparable in the T class
Is there any benefits as to which of the above 4 techniques should be used, is there any performance associated.
I know it is better maintainable and read if we implement IComparable in our class and if he have multiple sorting criteria we can have IComparer.
Thanks everyone for your views.
Regards,
Sakshi
推荐答案
首先,
1和2不是相当于3和4
除此之外 - 这些函数的性能主要取决于排序算法本身,无论实现如何,这都是基本概念之一计算机科学学士
关于委托和lambda表达式的
,给我们你的用法,然后我们可以分析这个例子的性能。
3.和4. - 并且作为一个拇指规则:编写良好的代码(大多数时候)将优于现成的实现。
然而,你有维护和升级想到...
祝你好运,
江户
Firstly,
1 and 2 are not comparable to 3 and 4
and besides - the performance of such functions will be determined mostly by the sorting algorithm itself, regardless of the implementation, this is one of the basic concepts of Computer Science
regarding delegate and lambda expressions, give us your usage and then we can relate to analyzing the performance of that example.
3. and 4. - and as a thumb rule: A well written code will (most of the times) top the performance of a off-the-shelf implementation.
However, then you have maintenance and upgrading to think of...
Good Luck,
Edo
我相信排序是在Miscrosoft内部排序中实现的。
我们只提供比较规则
考虑案例
I believe the sorting is implemented in the Sort internally by Miscrosoft.
We just provide the comparison rule
Consider the case
public class Employee
{
public string Name{get;set;}
public string Age{get;set;}
}
//Consider the list
//Say we populated the list with Employees
List<employee> employees = new List<employee>();
//1-Using Delegate
employees.Sort(delegate(Employee e1, Employee e2) { return e1.age.CompareTo(e2.age); });
//2-Using Lambda
employees.Sort((e1, e2) => e1.age.CompareTo(e2.age));
//3-Implement Icomparable in Employee class and write the same logic in Employee class //method CompareTo. Then we can call employees.Sort()
//4-Same for Icomparer as in comment 3
</employee></employee>
所以我相信排序算法是在内心深处写的。 NET内部(据我所知它实现了稳定的QuickSort算法),我们提供的是分类标准。
我的问题是我们可以使用任何一个4以上还是有一个好的,更好的和最好的
所以当你在谈论排序算法的时候,我可以看到我几乎没有做任何事情的余地。
感谢Edo的回复和时间。
问候,
Sakshi
So I believe the sorting algo is written deep down in the .NET internals(as far as I know it implements a stable QuickSort algo), what we are providing is the sorting criteria.
My question is can we use any of the above 4 or is there a good, better and best in them
So when you are saying about the sorting algoritm there I can see I have little scope to do anything.
Thanks Edo for your reply and time.
Regards,
Sakshi
这篇关于在通用列表中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!