ArrayList非泛型集合中的排序

使用ArrayList集合对字符串类型和封装类进行排序
这是封装类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList的排序
{
    class Person :IComparable
    {
        private string name;
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
        public int Age
        {
            get;
            set;
        }

        public int CompareTo(object obj)
        {
            Person p = obj as Person;
            //需要特别注意对null的处理,微软默认null最小,所以直接返回-1就ok
            if (p!=null)
            {
                return this.Name.Length - p.Name.Length;
            }
            return -1;
        }
        public Person(string name,int age)
        {
            this.Name = name;
            this.Age = age;
        }
        //对ToString方法进行重写,方便打印
        public override string ToString()
        {
            return this.Name + this.Age;  
        }
    }
}


这是主类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList的排序
{
    class Program
    {
        static void Main(string[] args)
        {

            #region arrayList对字符串的排序
            //ArrayList arrayList = new ArrayList();
            //arrayList.Add(new string(new char[] { 'a', 'b', 'c' }));
            //arrayList.Add("abd");
            //arrayList.Add("xyz");
            //arrayList.Add("xxx");
            //arrayList.Sort();
            //for (int i = 0; i < arrayList.Count; i++)
            //{
            //    Console.WriteLine(arrayList[i]);
            //}
            //Console.ReadKey(); 
            #endregion


            #region 使用arraylist进行对封装类的排序。
            ArrayList arrayList = new ArrayList();
            Person p1 = new Person("刘霞", 18);
            Person p2 = new Person("liuliuliu", 19);
            Person p3 = new Person("liuliu", 23);


            arrayList.Add(p1);
            arrayList.Add(p2);
            arrayList.Add(p3);
            arrayList.Add(null);
            arrayList.Add(null);

            //不使用比较器的重载方法
            //arrayList.Sort();

            //使用比较器的重载方法
            arrayList.Sort(new PersonSortByName());

            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine(arrayList[i]);//在Person类中进行了对ToString方法的重写,否则返回的是类名。
            }
            Console.ReadKey();
            #endregion


        }
    }
}

这是比较器类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ArrayList的排序
{
    //实现了IComparer的接口的类是一个比较器,是ArrayList集合中Sort方法的一个重载方法。
    //比较器类
    class PersonSortByName : IComparer
    {
        public int Compare(object x, object y)
        {
            Person p1 = x as Person;
            Person p2 = y as Person;
            //需要特别注意对null的处理,微软默认null最小,所以直接返回-1就ok
           if(p1!=null && p2 != null)
            {
                return p1.Name.Length - p2.Name.Length;
           }
            else
            {
                return -1;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值