首页 > 代码库 > ArrayList排序

ArrayList排序

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6  7 namespace ArrySort 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             ArrayList arry = new ArrayList() { new Person() { Name = "apple", Age = 12 }, new Person() { Name = "dog", Age = 64 }, new Person() { Name = "cat", Age = 41 } };14             Console.WriteLine("排序之前");15             for (int i = 0; i < arry.Count; i++)16             {17                 Console.WriteLine(((Person)arry[i]).Name);18             }19             arry.Sort();20             Console.WriteLine("排序之后");21             for (int i = 0; i < arry.Count; i++)22             {23                 Console.WriteLine(((Person)arry[i]).Name);24             }25             Console.ReadKey();26         }27 28         public class Person : IComparable//实现IComparable接口,则可以调用排序方法Sort(),否则对于有多个参数的对象,Sort()方法不知道该具体比较哪一项29         {30             public string Name { get; set; }31             public int Age { get; set; }32 33             34             public int CompareTo(object obj)//返回值>0表示比比较对象大,=0跟比较对象相等,<0比比较对象小35             {36                 Person p = obj as Person;//因为传入的是object类型,所以要将传入的参数转换为需要比较的类型37                 if (p == null)38                 {39                     throw new ArgumentException();//参数无效异常40                 }41                 else42                 {43                     //return this.Age - p.Age;//可以自定义按照类型的某一属性进行比较44                     return this.Name.Length - p.Name.Length;45                 }46             }47         }48     }49 }

实现IComparable接口,则ArrayList可以调用排序方法Sort(),否则对于有多个参数的对象,Sort()方法不知道该具体比较哪一项

 

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6  7 namespace UseOfICompare 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             ArrayList array = new ArrayList() {new Person(){Name = "Alice",Age = 42},new Person(){Name = "dog",Age = 62},new Person(){Name = "cat",Age = 15}};14             Console.WriteLine("比较前");15             for (int i = 0; i < array.Count; i++)16             {17                 Console.WriteLine(((Person)array[i]).Name);18             }19 20             //array.Sort(new CompareByNameLength());21             array.Sort(new CompareByAge());22 23             Console.WriteLine("比较之后");24             for (int i = 0; i < array.Count; i++)25             {26                 Console.WriteLine(((Person)array[i]).Name);27             }28             Console.ReadKey();29         }30     }31 32     public class Person33     {34         public string Name { get; set; }35         public int Age { get; set; }36     }37 38     public class CompareByNameLength : IComparer//实现IComparer接口,其实是做了一个比较器,比较器专门用来比较某一项,将比较器作为参数传入ArrayList的排序Sort()方法就可以实现比较了39     {40         public int Compare(object x, object y)//同样返回值是>0,<0,=041         {42             Person p1 = x as Person;43             Person p2 = y as Person;44             if (p1 == null && p2 == null)45             {46                 throw new ArgumentException();//参数类型错误异常47             }48             else49             {50                 return p1.Name.Length - p2.Name.Length;//按姓名长度比较51             }52         }53     }54 55     public class CompareByAge : IComparer56     {57         public int Compare(object x, object y)58         {59             Person p1 = x as Person;60             Person p2 = y as Person;61             if (p1 == null && p2 == null)62             {63                 throw new ArgumentException();//参数类型错误64             }65             else66             {67                 return p1.Age - p2.Age;68             }69         }70     }71 72 }

就一个Person对象来说,如果有两个属性,分别为Name和Age,如果某个时段想用Name长度来作为比较,某个时段想用Age大小来作为比较,如果让Person实现ICompareable接口就不太合适了,因为实现ICompareable接口再调用ArrayList的Sort()方法对按照在比较Person类中定义的规则进行一项的比较。

public int Compare(object x, object y)方法中传入两个对象是内部实现,只需要调用array.sort(new XXXXX());就可以了。

ArrayList排序