码迷,mamicode.com
首页 > 编程语言 > 详细

C# List<object> 按特定字段排序

时间:2015-01-19 20:54:26      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace ListObjectSort
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<Student> list = new List<Student>();
            Student stu1 = new Student()
            {
                Number = 4,
                Name = "石敏媛"
            };
            list.Add(stu1);

            Student stu2 = new Student()
            {
                Number = 2,
                Name = "梁秀秀"
            };
            list.Add(stu2);

            Student stu3 = new Student()
            {
                Number = 3,
                Name = "苏敏敏"
            };
            list.Add(stu3);

            Student stu4 = new Student()
            {
                Number = 1,
                Name = "褚丽丽"
            };
            list.Add(stu4);
            DisplayInfomation(list);

            Comparison<Student> compari = new Comparison<Student>(Compart);
            list.Sort(compari);
            DisplayInfomation(list);
        }

        public static void DisplayInfomation(List<Student> list)
        {
            Console.WriteLine("--------------------------");
            foreach (Student stu in list)
            {
                Console.WriteLine(stu.Number + "\t\t" + stu.Name);
            }
            Console.WriteLine("--------------------------");
        }

        public static int Compart(Student stu1, Student stu2)
        {
            int result = 0;
            CaseInsensitiveComparer objCompare = new CaseInsensitiveComparer();
            result = objCompare.Compare(stu1.Number, stu2.Number);
            return result;
        }
    }


    public class Student
    {
        public int Number { get; set; }
        public string Name { get; set; }
    }
}

 

C# List<object> 按特定字段排序

标签:

原文地址:http://www.cnblogs.com/mtsl/p/4234540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!