大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#核心语法

C#核心语法(16)

关键词:语法核心  阅读(5487) 赞(20)

[摘要]本文讲解了C#的核心语法,下面开讲。

16.排序(IComparable与IComparer)

(1)基本数据类型的排序

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = {2,3,6,9,8,4,7,5,0};

            Array.Sort(array);//排序方法

            foreach(int i in array)

            {

                Console.Write(i+" ");

            }

        }

}

上面的程序运行结果:0 2 3 4 5 6 7 8 9

(2)自定义的类型的排序(实现IComparable接口)

    class A:IComparable

    {

        private string name;

        private int age;

        public A(string name, int age)

        {

            this.name = name;

            this.age = age;

        }

        public int CompareTo(object obj)//实现比较的方法

        {

            A temp;

            if(obj is A)

            {

                temp=obj as A;

            }

            else

            {

                throw new ArgumentException("类型不一致!");

            }

            return this.name.CompareTo(temp.name);//根据姓名排序

        }

        public override string ToString()

        {

            return "[" + name + " " + age + "]";

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            A[] array =new A[3];

            array[0] = new A("李志伟",20);

            array[1] = new A("张三", 19);

            array[2] = new A("李四", 17);

            Array.Sort(array);//排序方法,对自定义类型进行排序

            foreach(A i in array)

            {

                Console.Write(i+" ");

            }

        }

}

(3)为自定义的类定义排序类(IComparer接口)

    class A

    {

        private string name;

        private int age;

        public A(string name, int age)

        {

            this.name = name;

            this.age = age;

        }

        public string Name

        {

            get { return name; }

 

        }

        public int Age

        {

            get { return age; }

        }

        public override string ToString()

        {

            return "[" + name + " " + age + "]";

        }

    }

    class B : IComparer//专为类A设计的排序类

    {

        private static B b;

        private B()

        {

        }

        public static B GetB()

        {

            if (b == null)

            {

                b = new B();

            }

            return b;

        }

        public int Compare(Object x, Object y)//实现排序方法

        {

            A xa, ya;

            if ((x is A) && (y is A))

            {

                xa = x as A;

                ya = y as A;

            }

            else

            {

                throw new ArgumentException("类型不一致!");

            }

            return xa.Name.CompareTo(ya.Name);//根据姓名排序

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            A[] array =new A[3];

            array[0] = new A("李志伟",20);

            array[1] = new A("张三", 19);

            array[2] = new A("李四", 17);

            Array.Sort(array,B.GetB());//排序方法

            foreach(A i in array)

            {

                Console.Write(i+" ");

            }

        }

    }



相关评论