C#中自定义排序的三种用法

本文详细介绍了如何在C#中对自定义类型进行排序,包括实现IComparable接口、使用委托回调与Lambda表达式以及实现IComparer接口的三种方法。通过示例展示了针对学生类按ID和年龄排序的完整代码,适用于理解C#中的自定义排序逻辑。
摘要由CSDN通过智能技术生成

自定义sort三种用法

1 使用接口IComparable
2 使用委托回调和lambad表达式
3 使用接口IComparer

我们先定义一个学生类

	public class Student
	{
	   //学生的编码
	    public int Id { get; set; }
	    //学生的年龄
	    public int Age { get; set; }
	}

排序规则
1 ID小的排前面
2 ID相同的,Age小的排前面

第一种(实现IComparable接口)

    public class Student : IComparable<Student>
    {
        //学生的编码
        public int Id { get; set; }
        //学生的年龄
        public int Age { get; set; }

        //排序规则
        public int CompareTo(Student other)
        {
            if (Id != other.Id) return Id.CompareTo(other.Id);
            return Age.CompareTo(other.Age);
        }
    }

完整代码

using System;
using System.Collections.Generic;

namespace SortDemo
{
    class Program
    {
        static void Main()
        {
            List<Student> students = new()
            {
                new(){Id = 2, Age = 16},
                new(){Id = 1, Age = 18},
                new(){Id = 1, Age = 12}
            };

            students.Sort();

            foreach (var item in students)
            {
                Console.WriteLine("ID : " + item.Id);
                Console.WriteLine("Age: " + item.Age);
            }
        }
    }

    public class Student : IComparable<Student>
    {
        //学生的编码
        public int Id { get; set; }
        //学生的年龄
        public int Age { get; set; }

        //排序规则
        public int CompareTo(Student other)
        {
            if (Id != other.Id) return Id.CompareTo(other.Id);
            return Age.CompareTo(other.Age);
        }
    }
}

第二种(使用委托)

用法

//使用lambad表达式
students.Sort((a, b) => 
            {
                if (a.Id != b.Id) return a.Id.CompareTo(b.Id);
                return a.Age.CompareTo(b.Age);
            });

完整代码

using System;
using System.Collections.Generic;

namespace SortDemo
{
    class Program
    {
        static void Main()
        {
            List<Student> students = new()
            {
                new(){Id = 2, Age = 16},
                new(){Id = 1, Age = 18},
                new(){Id = 1, Age = 12}
            };

            students.Sort((a, b) => 
            {
                if (a.Id != b.Id) return a.Id.CompareTo(b.Id);
                return a.Age.CompareTo(b.Age);
            });

            foreach (var item in students)
            {
                Console.WriteLine("ID : " + item.Id);
                Console.WriteLine("Age: " + item.Age);
            }
        }
    }

    public class Student
    {
        //学生的编码
        public int Id { get; set; }
        //学生的年龄
        public int Age { get; set; }
    }
}

第三种(使用接口IComparer)

实现一个StudentSort类

    public class StudentSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            if (x.Id != y.Id) return x.Id.CompareTo(y.Id);
            return x.Age.CompareTo(y.Age);
        }
    }

完整代码

using System;
using System.Collections.Generic;

namespace SortDemo
{
    class Program
    {
        static void Main()
        {
            List<Student> students = new()
            {
                new(){Id = 2, Age = 16},
                new(){Id = 1, Age = 18},
                new(){Id = 1, Age = 12}
            };

            IComparer<Student> comparer = new StudentSort();
            students.Sort(comparer);

            foreach (var item in students)
            {
                Console.WriteLine("ID : " + item.Id);
                Console.WriteLine("Age: " + item.Age);
            }
        }
    }

    public class Student
    {
        //学生的编码
        public int Id { get; set; }
        //学生的年龄
        public int Age { get; set; }
    }

    public class StudentSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            if (x.Id != y.Id) return x.Id.CompareTo(y.Id);
            return x.Age.CompareTo(y.Age);
        }
    }
}

结果
在这里插入图片描述
谢谢大家的观看0.0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值