首页 > 代码库 > c#比较器 排序

c#比较器 排序

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

class Program    {        static void Main(string[] args)        {            Data[] array = new Data[] { new Data() { a = 1 }, new Data() { a = 3 }, new Data() { a = 2 } };            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 1;            else if (a.a == b.a)                return 0;            else                return -1;        }        #endregion    }