使用List<T>容器提供的Sort(IComparer<T> comparer)方法,实现对象根据某个属性进行排序。说明如下:

1、自定义类Person

    class Person
    {
        public int id;
        public string name;
        public int age;
        public DateTime dateTime;
    }

2、创建时间比较器

    // 时间比较器
    class TimeComparer : IComparer<Person>
    {
        //实现升序
        public int Compare(Person x, Person y)
        {
            return (x.dateTime.CompareTo(y.dateTime));
        }
    }

IComparer是泛型接口,参数使用我们用来比较的对象的类型。同理,年龄比较器如下

// 年龄比较器
    class AgeComparer : IComparer<Person>
    {
        //实现升序
        public int Compare(Person x, Person y)
        {
            return (x.age.CompareTo(y.age));
        }
    }

3、创建List<Person>容器

 List<Person> listPerson = new List<Person>();

4、向容器中添加1000个对象

Person p ;
Random rd = new Random();
for(int i = 0; i < 1000; i++)
{
     p = new Person();
     p.id = i;
     p.name = "";
     p.age = rd.Next(10, 30);
     p.dateTime = DateTime.Now;
     listPerson.Add(p);
     Thread.Sleep(1);
}

5、调用sort方法排序

 listPerson.Sort(new TimeComparer());

6、排序结果

7、全部代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("打开软件\r\n");
             
            List<Person> listPerson = new List<Person>();
            Person p ;
            Random rd = new Random();
            Stopwatch sp = new Stopwatch();
            for(int i = 0; i < 1000; i++)
            {
                p = new Person();
                p.id = i;
                p.name = "";
                p.age = rd.Next(10, 30);
                p.dateTime = DateTime.Now;
                listPerson.Add(p);
                Thread.Sleep(1);
            }

            sp.Restart();
            // 按时间排序
            listPerson.Sort(new TimeComparer());

            // 按年龄排序
            //listPerson.Sort(new AgeComparer());
            sp.Stop();
            Console.WriteLine("排序耗时:"+ sp.ElapsedMilliseconds);
            Console.WriteLine("");

            for (int i = 0; i < listPerson.Count; i++)
            {
                Console.WriteLine("编号:"+listPerson[i].id);
                Console.WriteLine("年龄:"+listPerson[i].age);
                Console.WriteLine("时间:"+listPerson[i].dateTime.ToString("yyyy/MM/dd HH:mm:ss.ffff"));
                Console.WriteLine("");
            }
            Console.ReadLine();
            return;
        }
    }

    class Person
    {
        public int id;
        public string name;
        public int age;
        public DateTime dateTime;
    }

    // 年龄比较器
    class AgeComparer : IComparer<Person>
    {
        //实现升序
        public int Compare(Person x, Person y)
        {
            return (x.age.CompareTo(y.age));
        }
    }

    // 时间比较器
    class TimeComparer : IComparer<Person>
    {
        //实现升序
        public int Compare(Person x, Person y)
        {
            return (x.dateTime.CompareTo(y.dateTime));
        }
    }
}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐