1. 程式人生 > >List集合排序Sort(Comparison comparison)

List集合排序Sort(Comparison comparison)

List< T > 集合對複雜型別的排序Sort()有4個過載
1.T型別實現了IComparable< T >介面
2.另寫一個類,該類實現了IComparer< T >
3.Sort(Comparison< T > comparison),Comparison< T >為一個委託型別,CSDN定義如下:

public delegate int Comparison< T >(T x ,T y);
// 摘要: 
    //     表示比較同一型別的兩個物件的方法。
    //
    // 引數: 
    //   x:
// 要比較的第一個物件。 // y: // 要比較的第二個物件。 // 型別引數: // T: // 要比較的物件的型別。 // 返回結果: // 一個有符號整數,指示 x 與 y 的相對值,如下表所示。 值 含義 小於 0 x 小於 y。 0 x 等於 y。 大於 0 x 大於 y。

實現第三種方法的實現有3種途徑:

1.定義一個方法,定義一個委託例項,將該委託例項傳入Sort()中
eg.
 class Program
    {
        static void Main(string[] args)
        {
            Student sa1 = new
Student(1, "Washington", 33); Student sa2 = new Student(2, "Brown", 32); Student sa3 = new Student(4, "Wade", 67); Student sa4 = new Student(6, "Bosh", 47); List <Student> list = new List<Student> (){sa1 ,sa2 ,sa3 ,sa4 }; Comparison<Student> comparison = new
Comparison(SortByAge);//將方法傳給委託例項 list.Sort(comparison);//將委託傳入Sort() } //定義一個委託型別方法 static int SortByAge(Student x,Student y) { return x.Age.CompareTo(y.Age); } } public class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Type { get; set; } public Student(int id, string name, int age) { this.ID = id; this.Name = name; this.Age = age; } }
2.使用匿名委託
eg.
 class Program
    {
        static void Main(string[] args)
        {
            Student sa1 = new Student(1, "Washington", 33);
            Student sa2 = new Student(2, "Brown", 32);
            Student sa3 = new Student(4, "Wade", 67);
            Student sa4 = new Student(6, "Bosh", 47);
            List <Student> list = new List<Student> (){sa1 ,sa2 ,sa3 ,sa4 };
 //匿名委託delegate(Student x, Student y){return x.Name.CompareTo(y.Name);}
            list.Sort(delegate(Student x, Student y){return x.Name.CompareTo(y.Name);});
        }
        static int SortByAge(Student x,Student y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Type { get; set; }
        public Student(int id, string name, int age)
        {
            this.ID = id;
            this.Name = name;
            this.Age = age;
        }
    }
3.使用Lambda表示式
eg.
 class Program
    {
        static void Main(string[] args)
        {
            Student sa1 = new Student(1, "Washington", 33);
            Student sa2 = new Student(2, "Brown", 32);
            Student sa3 = new Student(4, "Wade", 67);
            Student sa4 = new Student(6, "Bosh", 47);
            List <Student> list = new List<Student> (){sa1 ,sa2 ,sa3 ,sa4 };
            //lambda表示式
            list.Sort((x,y) => x.ID.CompareTo(y.ID));
                foreach (Student s in listA )
            {
                Console.WriteLine(s.ID +" " + s.Name + " " + s.Age );
            } 
        }
    }
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Type { get; set; }
        public Student(int id, string name, int age)
        {
            this.ID = id;
            this.Name = name;
            this.Age = age;
        }
    }