C# 中 List.Sort运用(IComparer<T>)排序用法
2013-09-28 16:36
1266 查看
在项目中由于要解析一个箱单号,要求从小到大的顺序。由于循环从数据库取出来取得值不是按照从小到大排序的因此需要自己转换下。
本篇文章用到了 ListTSort 方法 (IComparerT)方法重载
http://msdn.microsoft.com/zh-cn/library/234b841s%28v=vs.110%29.aspx
使用指定的比较器对整个 List<T> 中的元素进行排序。
命名空间: System.Collections.Generic
程序集: mscorlib(在 mscorlib.dll 中)
语法
C#
比较元素时要使用的 IComparer<T> 实现,或者为null,表示使用默认比较器Comparer<T>.Default。
异常
备注
如果提供了 comparer,则List<T> 的元素是使用指定的IComparer<T>
实现进行排序的。
如果 comparer 为
null,则默认比较器Comparer<T>.Default
将检查类型T 是否实现了 IComparable<T> 泛型接口,如果实现了该接口,则使用该实现。否则,Comparer<T>.Default
将检查类型T 是否实现了 IComparable 接口。如果类型
T 未实现任一接口,则 Comparer<T>.Default 将引发InvalidOperationException。
此方法使用 Array.Sort,后者使用 QuickSort 算法。此实现执行不稳定排序;亦即,如果两元素相等,则其顺序可能不被保留。
相反,稳定排序则会保持相等元素的顺序。
一般情况下,此方法的运算复杂度为 O(n logn),其中
n 为 Count,最坏的情况下其运算复杂度为 O(n ^ 2)。
使用示例:
Sort排序后得到的如下效果:
字符串中带数字情况:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/15/761e0ceb45fe2a18a9b1c783136288fe)
纯数字情况排序:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/15/dd46a48b8113f4a8a2822b1db8536be7)
完全字母排序:不支持。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/15/af3664ec6bc5b0e828cb8a0708697794)
其他参考文章 http://www.cnblogs.com/ywqu/archive/2009/01/21/1379186.html http://hi.baidu.com/bin545/item/972ca9f444dc360dd89e72d4 http://blog.csdn.net/think_soft/article/details/3446393
本篇文章用到了 ListTSort 方法 (IComparerT)方法重载
List<T>.Sort 方法 (IComparer<T>)的方法解释地址如下:
http://msdn.microsoft.com/zh-cn/library/234b841s%28v=vs.110%29.aspx
使用指定的比较器对整个 List<T> 中的元素进行排序。命名空间: System.Collections.Generic
程序集: mscorlib(在 mscorlib.dll 中)
语法
C#
public void Sort( IComparer<T> comparer )
参数
comparer类型:System.Collections.Generic.IComparer<T>比较元素时要使用的 IComparer<T> 实现,或者为null,表示使用默认比较器Comparer<T>.Default。
异常
异常 | 条件 |
---|---|
InvalidOperationException | comparer 为 null,且默认比较器Comparer<T>.Default 找不到T 类型的 IComparable<T> 泛型接口或IComparable 接口的实现。 |
ArgumentException | comparer 的实现导致排序时出现错误。例如,将某个项与其自身进行比较时,comparer 可能不返回 0。 |
如果提供了 comparer,则List<T> 的元素是使用指定的IComparer<T>
实现进行排序的。
如果 comparer 为
null,则默认比较器Comparer<T>.Default
将检查类型T 是否实现了 IComparable<T> 泛型接口,如果实现了该接口,则使用该实现。否则,Comparer<T>.Default
将检查类型T 是否实现了 IComparable 接口。如果类型
T 未实现任一接口,则 Comparer<T>.Default 将引发InvalidOperationException。
此方法使用 Array.Sort,后者使用 QuickSort 算法。此实现执行不稳定排序;亦即,如果两元素相等,则其顺序可能不被保留。
相反,稳定排序则会保持相等元素的顺序。
一般情况下,此方法的运算复杂度为 O(n logn),其中
n 为 Count,最坏的情况下其运算复杂度为 O(n ^ 2)。
使用示例:
public class LotAtt10Comparer : IComparer<string> { public int Compare(string x, string y) { if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { // ...and y is not null, compare the // lengths of the two strings. // int retval = x.Length.CompareTo(y.Length); if (retval != 0) { // If the strings are not of equal length, // the longer string is greater. // return retval; } else { // If the strings are of equal length, // sort them with ordinary string comparison. // return x.CompareTo(y); } } } } }
Sort排序后得到的如下效果:
字符串中带数字情况:
List<string> strList = new List<string>(); strList.Add("A2013092289-(1)"); strList.Add("A2013092289-(13)"); strList.Add("A2013092289-(14)"); strList.Add("A2013092289-(15)"); strList.Add("A2013092289-(2)"); strList.Add("A2013092289-(20)"); strList.Add("A2013092289-(3)"); strList.Sort(compar);
纯数字情况排序:
List<string> dinosaurs = new List<string>(); dinosaurs.Add("1"); dinosaurs.Add("32"); dinosaurs.Add("43"); dinosaurs.Add("4"); dinosaurs.Sort(compar);
完全字母排序:不支持。
List<string> strIng = new List<string>(); strIng.Add("Pachycephalosaurus"); strIng.Add("Amargasaurus"); strIng.Add("Mamenchisaurus"); strIng.Add("Deinonychus"); strIng.Sort(compar);
其他参考文章 http://www.cnblogs.com/ywqu/archive/2009/01/21/1379186.html http://hi.baidu.com/bin545/item/972ca9f444dc360dd89e72d4 http://blog.csdn.net/think_soft/article/details/3446393
相关文章推荐
- C#画图
- C#全角半角转换函数
- visual studio控件datagridview的操作
- c#操作通过button控制viewtree节点的改变
- C# Delegate
- c#操作word的方法集成,绝对可靠
- c#通过标签读取word段落表格(解析无法操作读取的真正原因)
- C#Windows服务:一些方法(启动、停止等)
- 用最简单的方式在C#中使用多线程加速耗时的图像处理算法的执行(多核机器)。
- c#初学简单程序实例
- C#中的委托和事件(续)
- C# 中的委托和事件
- 关闭ReSharper中的[ Use 'var' ]提示(Disable C# “var” Recommendation in ReSharper)
- c# datatable 有用信息整理(不定时更新)
- c#的owner
- 【C#中抽象类和接口的使用例子】
- c# 多项目(应用程序)共用一个Config配置文件
- c#获取新浪微博登录cookie
- C#中的@符号
- .net-C#代码判断