C#排序函数和自定义比较器

在C#中,要比较两个数组,可以调用

System.Array.Sort(...)方法

List等也有Sort()方法

 

有两种方式可以为自己的类提供排序;

 

1.类实现接口 IComparable

2.创建比较器类,该类实现接口IComparer

 

第二种方法的优点是,你可以在不修改已有的类,创建自己的比较器类,而且能实现比第一种方法更加灵活的方式;


 //rorger,2011
//IComparable & IComparer
//using Sysem.Collections  required
using System;
using System.Linq;
using System.Text;
using System.Collections;

namespace AnimalCompare
{
    class Program
    {
        public class Animal:IComparable
        {

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

            public Animal(string name,int age )
            {
                this.name = name;
                this.age = age;
            }

            public int CompareTo(Object obj)
            {
                Animal other = obj as Animal;
                int result = name.CompareTo(other.name);
                 if (result ==0)
                 {
                     result = age.CompareTo(other.age);
                 }
                 return result;
            }
            public override string ToString()
            {
                return string.Format("{0},{1}", name, age);
            }
        }


        public class AnimalComparer:IComparer
        {
               public int Compare(object lhs,object rhs)
               {
                   Animal aniLhs = lhs as Animal;
                   Animal aniRhs = rhs as Animal;
                   int result = aniLhs.Age.CompareTo(aniRhs.Age);
                   if (result == 0)
                   {
                       result = aniLhs.Name.CompareTo(aniRhs.Name);
                   }

                   return result;
               }
        }

        static void Main(string[] args)
        {
            Animal[] animals = new Animal[]{
                new Animal("ElephantBig",5),
                new Animal("ElephantBig",3),
                new Animal("ElephantNano",1)
            };

            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal);
            }
            Console.WriteLine("/nAfter soring...");
            Array.Sort(animals);
            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal);
            }

            Console.WriteLine("/nUsing IComparer...");
            Array.Sort(animals, new AnimalComparer());
            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
    }
}

/*
 *ouput:
 ElephantBig,5
ElephantBig,3
ElephantNano,1

After soring...
ElephantBig,3
ElephantBig,5
ElephantNano,1

Using IComparer...
ElephantNano,1
ElephantBig,3
ElephantBig,5
Press any key to continue . . .
*/


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值