您的位置:首页 > 编程语言 > C#

C# 中 List.Sort运用(IComparer<T>)排序用法

2013-09-28 16:36 1266 查看
在项目中由于要解析一个箱单号,要求从小到大的顺序。由于循环从数据库取出来取得值不是按照从小到大排序的因此需要自己转换下。

本篇文章用到了 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

异常

异常条件
InvalidOperationExceptioncomparer 为
null,且默认比较器Comparer<T>.Default
找不到T 类型的 IComparable<T> 泛型接口或IComparable
接口的实现。

ArgumentExceptioncomparer 的实现导致排序时出现错误。例如,将某个项与其自身进行比较时,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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: