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

namespace ConsoleApplication1
{
    class Student: IComparable
    {
        private string _name;
        private int _age;

        public Student(string name, int age)
        {
            this._name = name;
            this._age = age;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }

        int IComparable.CompareTo(object obj)
        {
            if(!(obj is Student))
            {
                throw new Exception("obj must be Student.");
            }

            return this._name.CompareTo(((Student)obj)._name);
        }

        // improved
        public int CompareTo(Student obj)
        {

            return this._name.CompareTo(obj._name);
        }

        public override string ToString()
        {
            return _name + " age: " + _age.ToString();
        }
    }
    
    class Program
    {
       
        static void Main(string[] args)
        {
           Student[] ss = new Student[5];
           ss[0] = new Student("Zhu", 15);
           ss[1] = new Student("Jim", 12);
           ss[2] = new Student("Kate", 19);
           ss[3] = new Student("Kay", 22);
           ss[4] = new Student("Xi", 21);

            Array.Sort(ss);
            foreach(Student s in ss)
            {
                Console.WriteLine(s.ToString());
            }
            
            Console.ReadKey();
        }
    }
}
View Code

 

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

namespace ConsoleApplication1
{
    class Student: IComparable
    {
        private string _name;
        private int _age;

        public Student(string name, int age)
        {
            this._name = name;
            this._age = age;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }

        int IComparable.CompareTo(object obj)
        {
            if(!(obj is Student))
            {
                throw new Exception("obj must be Student.");
            }

            return this._name.CompareTo(((Student)obj)._name);
        }

        // improved
        public int CompareTo(Student obj)
        {

            return this._name.CompareTo(obj._name);
        }

        public override string ToString()
        {
            return _name + " age: " + _age.ToString();
        }


        // IComparar
        private static AgeComparer ageCom = null;
        public static IComparer AgeCom
        {
            get
            {
                if(ageCom == null)
                {
                    ageCom = new AgeComparer();
                }

                return ageCom; 
            }
        }

        // internal class
        private class AgeComparer: IComparer
        {
            int IComparer.Compare(object x, object y)
            {
                if (!(x is Student) || !(y is Student))
                {
                    throw new Exception("objs must be Student.");
                }

                return ((Student)x)._age.CompareTo(((Student)y)._age);
            }
        }
    }
    
    class Program
    {
       
        static void Main(string[] args)
        {
           Student[] ss = new Student[5];
           ss[0] = new Student("Zhu", 15);
           ss[1] = new Student("Jim", 12);
           ss[2] = new Student("Kate", 19);
           ss[3] = new Student("Kay", 22);
           ss[4] = new Student("Xi", 21);

            // sort according to name
            Array.Sort(ss);
            foreach(Student s in ss)
            {
                Console.WriteLine(s.ToString());
            }

            Console.WriteLine();

            // sort according to age
            Array.Sort(ss, Student.AgeCom);
            foreach (Student s in ss)
            {
                Console.WriteLine(s.ToString());
            }
            
            Console.ReadKey();
        }
    }
}
View Code

Note: 内部private Class。

posted on 2018-06-01 14:21  zzunstu  阅读(113)  评论(0编辑  收藏  举报