c# 根据对象的属性进行排序

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

1、自定义类Person

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

2、创建时间比较器

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

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

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

3、创建List容器

 List listPerson = new List();

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、排序结果

c# 根据对象的属性进行排序_第1张图片

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 listPerson = new List();
            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
    {
        //实现升序
        public int Compare(Person x, Person y)
        {
            return (x.age.CompareTo(y.age));
        }
    }

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

 

你可能感兴趣的:(我的练习项目)