注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

数组、索引器和集合(七)  

2010-12-25 17:47:30|  分类: C# & .NET |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

实现IComparable接口

ListSort()方法通常能对常见数据类型实现排序,如果要实现用户自定义类的排序,该类就必须实现Icomparable接口。例如定义了一个Student的类,现在按学号的大小顺序实现排序,代码如下:

using System;

using System.Collections.Generic;

using System.Text;

namespace Castor

{

    class Student:IComparable<Student>

    {

        private double ID;

        private string Name;

        private int Age;

        private bool IsMale;

        public Student()

        {

            ID = 0;

            Name = "NoName";

            Age = 0;

            IsMale = true;

        }

 

        public Student(double id, string name, int age, bool isMale)

        {

            ID = id;

            Name = name;

            Age = age;

            IsMale = isMale;

        }

        //实现IComparable接口

        public int CompareTo(Student s)

        {

            return this.ID.CompareTo(s.ID);

        }

        //重载ToString方法

        public override string ToString()

        {

            string Sex = "Female";

            if (this.IsMale)

                Sex = "Male";

            string ret = string.Format("ID:{0}\tName:{1}\tAge:{2}\tSex:{3}", ID, Name, Age, Sex);

            return ret;

        }

        static void Main(string[] args)

        {

            Student s1 = new Student(2010202080006, "Jack", 23, true);

            Student s2 = new Student(2010202080001, "Tom", 24, true);

            Student s3 = new Student(2010202080004, "Lisa", 23, false);

            Student s4 = new Student(2010202080002, "Lily", 22, false);

            List<Student> Ls = new List<Student>();

            Ls.Add(s1);

            Ls.Add(s2);

            Ls.Add(s3);

            Ls.Add(s4);

            Ls.Sort();

            foreach (Student s in Ls)

            {

                Console.WriteLine(s.ToString());

            }

            Console.Read();

        }

    }

}

运行后效果如下:

2010年12月25日 - Castor - 趁年轻,多折腾~~

可见成功完成Student类的排序。

不过,有些时候并不总是想按学号进行排序的,例如使用姓名,或者年龄,谁知道呢,反正用户的需求是千变万化的,这个时候可以通过IComparer实现。

通过创建自己的IComparer实现,并将其传给ListSort()方法,完成不同的排序需求。假设这里用三种排序来实现。

首先需要定义一个内部类,该内部类中定一个排序的枚举,这里显然是IDNameAge

public class StudentComparer : IComparer<Student>

{

    public enum CompareType { ID, Name, Age };

    public Student.StudentComparer.CompareType CompareWhat;

    public int Compare(Student s1, Student s2)

    {

        return s1.CompareTo(s2, CompareWhat);

    }

}//内嵌类

该内部类实现了接口IComparer中的Compare方法。在对主类排序的时候,通过将排序类型传递到Sort方法中去,实现不同的排序,完整的程序如下:

using System;

using System.Collections.Generic;

using System.Text;

namespace Castor

{

    class Student:IComparable<Student>

    {

        private double ID;

        private string Name;

        private int Age;

        private bool IsMale;

        public Student()//默认构造函数

        {

            ID = 0;

            Name = "NoName";

            Age = 0;

            IsMale = true;

        }

        //构造函数

        public Student(double id, string name, int age, bool isMale)

        {

            ID = id;

            Name = name;

            Age = age;

            IsMale = isMale;

        }

        //实现IComparer的内嵌类

        public class StudentComparer : IComparer<Student>

        {

            public enum CompareType { ID, Name, Age };

            public Student.StudentComparer.CompareType CompareWhat;

            public int Compare(Student s1, Student s2)

            {

                return s1.CompareTo(s2, CompareWhat);

            }

        }//内嵌类

        //获取Comparer的静态方法

        public static StudentComparer GetComparer()

        {

            return new Student.StudentComparer();

        }

        //实现IComparable接口

        public int CompareTo(Student s)

        {

            return this.ID.CompareTo(s.ID);

        }//实现接口,支持Sort()

        //自定义的比较实现

        public int CompareTo(Student s, StudentComparer.CompareType what)

        {

            switch (what)

            {

                case StudentComparer.CompareType.ID:

                    return this.ID.CompareTo(s.ID);

                case StudentComparer.CompareType.Name:

                    return this.Name.CompareTo(s.Name);

                case StudentComparer.CompareType.Age:

                    return this.Age.CompareTo(s.Age);

            }

            return 0;

        }

        //重载ToString方法

        public override string ToString()

        {

            string Sex = "Female";

            if (this.IsMale)

                Sex = "Male";

            string ret = string.Format("ID:{0}\tName:{1}\tAge:{2}\tSex:{3}", ID, Name, Age, Sex);

            return ret;

        }

        //主程序入口

        static void Main(string[] args)

        {

            Student s1 = new Student(2010202080006, "Jack", 23, true);

            Student s2 = new Student(2010202080001, "Tom", 24, true);

            Student s3 = new Student(2010202080004, "Lisa", 23, false);

            Student s4 = new Student(2010202080002, "Lily", 22, false);

            List<Student> Ls = new List<Student>();

            Ls.Add(s1);

            Ls.Add(s2);

            Ls.Add(s3);

            Ls.Add(s4);

            StudentComparer sc = Student.GetComparer();

            Console.WriteLine("ID排序:");

            sc.CompareWhat = StudentComparer.CompareType.ID;

            Ls.Sort(sc);

            PrintAll(Ls);

            Console.WriteLine("Name排序:");

            sc.CompareWhat = StudentComparer.CompareType.Name;

            Ls.Sort(sc);

            PrintAll(Ls);

            Console.WriteLine("Age排序:");

            sc.CompareWhat = StudentComparer.CompareType.Age;

            Ls.Sort(sc);

            PrintAll(Ls);

            Console.Read();

        }

        //输出List中的元素

        public static void PrintAll(List<Student> ls)

        {

            foreach (Student s in ls)

            {

                Console.WriteLine(s.ToString());

            }

        }

    }

}

运行效果如下: 

2010年12月25日 - Castor - 趁年轻,多折腾~~

 

  评论这张
 
阅读(479)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016