c#比较器 排序

时间:2022-03-09 14:12:05

原地址:http://blog.csdn.net/xutao_ustc/article/details/6314057

class Program
{
static void Main(string[] args)
{
Data[] array = new Data[] { new Data() { a = }, new Data() { a = }, new Data() { a = } };
Array.Sort(array,new MyComparer());
foreach (var item in array)
{
Console.WriteLine(item.a);
}
Console.ReadKey();
}
}
class Data
{
public int a{get;set;}
}
class MyComparer : IComparer {
#region IComparer Members
public int Compare(object x, object y)
{
Data a = x as Data;
Data b = y as Data;
if (a.a > b.a)
return ;
else if (a.a == b.a)
return ;
else
return -;
}
#endregion
}