ArrayList自定义排序的两种实现方式

ArrayList 这个集合的排序方式有两种:

1.实现ICompareable接口  实现自定义排序

2.自己定义一个类(比较器)实现arraylist的自定义排序

下面具体说明:

第一种:

class Person : IComparable//自定义类实现IComparable接口的CompareTo的方法,实现自定义排序,代码很简单,就不在仔细说了
    {
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int CompareTo(object obj)
        {
            //值 含义 小于零 此实例按排序顺序在 obj 前面。 零 此实例与 obj 在排序顺序中出现的位置相同。
            int result = 1;
            if (obj == null)//判断obj是否为null
            {
                result = 1;
            }
            else
            {
                Person p = obj as Person;//类型转换
                if (p == null)
                {
                    result = 1;
                }
                else
                {
                    if (p.Age > this.Age)
                    {
                        result = -1;
                    }
                    else if (p.Age == this.Age)
                    {
                        result = 0;
                    }
                    else
                    {
                        result = 1;
                    }
                }
            }

            return result;
        }
    }
//测试例子
            ArrayList list = new ArrayList();
            list.Add(new Person() { Name = "呵", Age = 12 });
            list.Add(new Person() { Name = "hehe", Age = 15 });
            list.Add(new Person() { Name = "嘻嘻", Age = 1 });
            list.Add(new Person() { Name = "哈哈", Age = 122 });
            list.Add(new Person() { Name = "嘿嘿", Age = 14 });
            list.Add(new Person() { Name = "哈喽", Age = 17 });
            list.Sort();
            foreach (Person p in list)
            {
                Console.WriteLine("姓名:{0}\t年龄:{1}", p.Name, p.Age);
            } 


 
  
2.再来看第二中
先自己定义一个类
 
  

    public class Person1
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
//在定义一个用于比较的类(比较器)
 public class So : System.Collections.IComparer  //实现IComparer接口的Compare方法,代码也很简单,不仔细说了
 
  

    {

        public int Compare(Object x, Object y)
        {
            // 值 含义  -1 x 小于 y。 零 x 等于 y。 1 x 大于 y。
            int result = 0;

            if (x == null)
            {
                result = -1;
            }
            else
            {
                if (y == null)
                {
                    result = 1;
                }
                else
                {
                    Person1 p1 = x as Person1;//类型转换
                    Person1 p2 = y as Person1;
                    if (p1 != null && p2 != null)
                    {
                        if (p1.Age > p2.Age)
                        {
                            result = 1;
                        }
                        else if (p1.Age < p2.Age)
                        {
                            result = -1;
                        }
                        else
                        {
                            result = 0;
                        }
                    }
                    else if (p1 == null)
                    {
                        result = -1;
                    }
                    else
                    {
                        result = 1;
                    }
                }

            }
            return result;
        }
    }

//测试例子
 
  
            ArrayList list = new ArrayList();
            list.Add(new Person1() { Name = "哈哈", Age = 12 });
            list.Add(new Person1() { Name = "嘻嘻", Age = 13 });
            list.Add(new Person1() { Name = "呵呵", Age = 11 });
            list.Add(new Person1() { Name = "撒旦法", Age = 2 });
            list.Add(new Person1() { Name = "宿舍", Age = 121 });
            list.Add(new Person1() { Name = "223", Age = 126 });
            So c = new So();
            list.Sort(c);
            foreach (Person1 p in list)
            {
                Console.WriteLine("姓名:{0}\t年龄:{1}", p.Name, p.Age);
            }  


 
  

 
  
 
  
 
  

 
 

你可能感兴趣的:(C#)