C# ?? List.Sort????(IComparer<T>)?????÷?
???????????? ???????[ 2013/10/8 14:59:47 ] ????????
????????????????????????????????С???????????????????????????????????????С????????????????????????
???????????????? 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> ???????????????y?????????????????Comparer<T>.Default ?????????T ???????? IComparable ??????????? T δ???????????? Comparer<T>.Default ??????InvalidOperationException??
???????????? Array.Sort????????? QuickSort ???????????в?????????????????????????????????????????? ??????????????????????????
???????????£???????????????? O(n logn)?????? n ? Count????????????????????? O(n ^ 2)??
????????????
view plaincopy
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);
}
}
}
}
}
|
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11