1. 程式人生 > 實用技巧 >13.集合中物件的預設排序

13.集合中物件的預設排序

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

namespace _3.集合中物件的預設排序
{
class Program
{
static void Main(string[] args)
{
//////////1.字串排序
List list = new List()
{
"小王","小李","小趙","小順","小孫","小劉","小紅","小唐","小阿","小夏"
};
Console.WriteLine("-----------string排序前----------");
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("-----------string排序後----------");
list.Sort();//直接排序元素
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("---------string排序後反轉--------------");
list.Reverse();
foreach (string item in list)
{
Console.WriteLine(item);
}

        ////////////////////2.值型別排序
        List<int> list_int = new List<int>()
        {
         20,21,67,18,62,82,20
        };
        Console.WriteLine("-----------list_int排序前---------------");
        foreach (int item in list_int)//遍歷
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("-----------list_int排序後---------------");
        list_int.Sort();//呼叫Sort()方法直接排序元素
        foreach (int item in list_int)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("-----------list_int排序後反轉-----------------");
        list_int.Reverse();
        foreach (int item in list_int)
        {
            Console.WriteLine(item);
        }


        ///////////2.物件的排序
        //物件要考慮用什麼來排序!!!!!
        //下面用學號來排序。還可以用學生姓名來排序。考慮一下。
        Student obj1 = new Student(100, "小王");
        Student obj2 = new Student(108, "小夏");
        Student obj3 = new Student(102, "小劉");
        Student obj4 = new Student(103, "小張");

        List<Student> listStu = new List<Student>() { obj1,obj2,obj3,obj4};
        Console.WriteLine("------------Student物件排序前-----------");
        foreach (Student item in listStu)
        {
            Console.WriteLine(item.StudentId + "\t" + item.StudentNmae + "\t" + item.Age);
        }
        Console.WriteLine("------------Student物件排序後-----------");
        listStu.Sort();
        foreach (Student item in listStu)
        {
            Console.WriteLine(item.StudentId + "\t" + item.StudentNmae + "\t" + item.Age);
        }
        Console.WriteLine("------------Student物件排序後反轉-----------");
        listStu.Reverse();
        foreach (Student item in listStu)
        {
            Console.WriteLine(item.StudentId + "\t" + item.StudentNmae + "\t" + item.Age);
        }

        Console.ReadKey();
    }
}

class Student : IComparable<Student>  //IComparable<>    預設的泛型比較器介面
{
    public Student()
    {

    }
    public Student(Int32 stuId, string stuNmae)
    {
        this.StudentId = stuId;
        this.StudentNmae = stuNmae;
    }

    public Int32 StudentId { get; set; }
    public string StudentNmae { get; set; }
    public Int32 Age { get; set; }

    /// <summary>
    /// 實現介面
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public int CompareTo(Student other)
    {
        return other.StudentId.CompareTo(this.StudentId);//降序排序
                                                         // return this.StudentId.CompareTo(other);//升序排序
    }

    /// <summary>
    /// 顯示實現介面
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    //int IComparable<Student>.CompareTo(Student other)
    //{
    //    throw new NotImplementedException();
    //}
}

}