C#List簡單排序

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentList = new List<Student>();
            StudentList.Add(new Student("張三",23));
            StudentList.Add(new Student("李四", 1));
            StudentList.Add(new Student("王五", 3));
            StudentList.Add(new Student("宋發", 15));
            StudentList.Add(new Student("劉明", 8));
            StudentList.Add(new Student("趙麗", 12));

            Console.WriteLine("排序前的輸出:");
            for (int i = 0; i < StudentList.Count; i++)
            {
                Console.WriteLine("姓名:" + StudentList.ElementAt(i).Name + ",序號:" + StudentList.ElementAt(i).ID);
            }
            Console.WriteLine();
            Console.WriteLine("升序排序後的輸出:");
            var ASCList = StudentList.OrderBy(p => p.ID).ToList();
            for (int i = 0; i < ASCList.Count; i++)
            {
                Console.WriteLine("姓名:" + ASCList.ElementAt(i).Name + ",序號:" + ASCList.ElementAt(i).ID);
            }
            Console.WriteLine();
            Console.WriteLine("降序排序後的輸出:");
            var DESCList = StudentList.OrderByDescending(p => p.ID).ToList();
            for (int i = 0; i < DESCList.Count; i++)
            {
                Console.WriteLine("姓名:" + DESCList.ElementAt(i).Name + ",序號:" + DESCList.ElementAt(i).ID);
            }
            Console.ReadLine();
        }
    }

    public class Student
    {
        public string Name;
        public int ID;

        public Student(string name,int id)
        {
            Name = name;
            ID = id;
        }
    }
}
</pre><pre name="code" class="csharp"><img src="https://img-blog.csdn.net/20150327205805758?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpc2hpamlhbmdiaWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章