吃螺丝

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

蛋疼  还是蛋疼  无聊写点基础东西

控制台程序

实现对一个类的排序

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

namespace ArrayEX
{
    public class Person : IComparable<Person>
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public Person(string LastName,string FirstName)
        {
            this.LastName = LastName;
            this.FirstName = FirstName;
        }

        public int CompareTo(Person obj)
        {   
            int result = this.LastName.CompareTo(obj.LastName);
            if (result == 0)
            {
                result = this.FirstName.CompareTo(obj.FirstName);
            }
            return result;
        }
    }

    public class PersonComparer : IComparer<PersonComparer>
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public PersonComparer(string LastName, string FirstName)
        {
            this.LastName = LastName;
            this.FirstName = FirstName;
        }
        public int Compare(PersonComparer x,PersonComparer y)
        {
            return x.LastName.CompareTo(y.LastName);
        }
    }
}

//////////////////////////////////////

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

namespace ArrayEX
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] persons ={
                                 new Person("Emerson","Fittipaldi"),
                                 new Person("Niki","Lauda"),
                                 new Person("Ayrton","Senna"),
                                 new Person("Michael","Schumacher"),
                             };
            Array.Sort(persons);
            foreach (Person p in persons)
            {
                Console.WriteLine("{0}\t{1}", p.LastName, p.FirstName);
            }

            Console.WriteLine("/////////////////////////");

            PersonComparer[] personcomparer ={
                                                new PersonComparer("Emerson","Fittipaldi"),
                                                new PersonComparer("Niki","Lauda"),
                                                new PersonComparer("Michael","Schumacher"),
                                                new PersonComparer("Ayrton","Senna"),
                                            };
            Array.Sort(personcomparer, personcomparer);
            foreach (PersonComparer p in personcomparer)
            {
                Console.WriteLine("{0}\t{1}", p.LastName, p.FirstName);
            }
            Console.Read();
        }
    }
}

 

所有数值类型(如 Int32Double)均实现 IComparable,这一点与 StringCharDateTime 是相同的。 此外,自定义类型还应提供自己的 IComparable 实现,以便允许对象实例进行排序。

IComparable 接口

此接口由具有可排序值的类型实现。 它要求实现类型定义单个方法 CompareTo(Object),该方法指示当前实例在排序顺序中的位置是位于同一类型的另一个对象之前、之后还是与其位置相同。 实例的 IComparable 实现由 Array.SortArrayList.Sort 等方法自动调用。

posted on 2010-12-31 16:49  吃螺丝  阅读(237)  评论(1编辑  收藏  举报