国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

對集合排序的三種方式

2019-11-17 02:28:02
字體:
供稿:網(wǎng)友

對集合排序的三種方式

對集合排序,可能最先想到的是使用OrderBy方法。

    class PRogram
    {
        static void Main(string[] args)
        {
            IEnumerable<Student> result = GetStudents().OrderBy(r => r.Score);
            foreach (var item in result)
            {
                Console.WriteLine(item.Name + "--" + item.Score);
            }
            Console.ReadKey();
        }
        private static List<Student> GetStudents()
        {
            return new List<Student>()
            {
                new Student(){Id = 1, Name = "張三",Age = 15, Score = 80},
                new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
                new Student(){Id = 3, Name = "趙武",Age = 14, Score = 90}
            };
        }
    }
    public class Student 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int Score { get; set; }
    }

以上,OrderBy返回的類型是IEnumerable<Student>。

如果想使用List<T>的Sort方法,就需要讓Student實現(xiàn)IComparable<Student>接口。

   class Program
    {
        static void Main(string[] args)
        {
            List<Student> result = GetStudents();
            result.Sort();
            foreach (var item in result)
            {
                Console.WriteLine(item.Name + "--" + item.Score);
            }
            Console.ReadKey();
        }
        private static List<Student> GetStudents()

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西峡县| 双流县| 北流市| 张家川| 崇义县| 扬州市| 阳城县| 来凤县| 澄迈县| 岚皋县| 太原市| 安新县| 德钦县| 塔河县| 浏阳市| 奉化市| 无极县| 呼图壁县| 六安市| 杭锦后旗| 鲜城| 镇坪县| 东乌珠穆沁旗| 偃师市| 怀柔区| 岐山县| 岫岩| 安新县| 二连浩特市| 罗平县| 南宁市| 灵川县| 淮北市| 密云县| 桃江县| 阿拉善左旗| 汕尾市| 怀来县| 平定县| 清苑县| 资溪县|