List排序 Sort的用法

目录

1.简要说明

2.官方文件

3.举例说明

3.1.默认排序Sort()

3.2Sort(IComparer comparer)

3.3Sort(Comparison comparison)

3.4Sort(int index, int count, IComparer comparer)

4.总结


1.简要说明

泛型List命名空间为System.Collections.Generic,官网文档的Sort有4种重载方法:

//使用指定的比较器对 System.Collections.Generic.List`1 中某个范围内的元素进行排序
public void Sort(int index, int count, IComparer<T> comparer);

//使用指定的 System.Comparison`1 对整个 System.Collections.Generic.List`1 中的元素进行排序
public void Sort(Comparison<T> comparison);

//使用默认比较器对整个 System.Collections.Generic.List`1 中的元素进行排序
public void Sort();

//使用指定的比较器对整个 System.Collections.Generic.List`1 中的元素进行排序
public void Sort(IComparer<T> comparer);

2.官方文件

        //
        // 摘要:
        //     使用指定的比较器对 System.Collections.Generic.List`1 中某个范围内的元素进行排序。
        //
        // 参数:
        //   index:
        //     要排序的范围的从零开始的起始索引。
        //
        //   count:
        //     要排序的范围的长度。
        //
        //   comparer:
        //     比较元素时要使用的 System.Collections.Generic.IComparer`1 实现,或者为 null,表示使用默认比较器 System.Collections.Generic.Comparer`1.Default。
        //
        // 异常:
        //   T:System.ArgumentOutOfRangeException:
        //     index 小于 0。 - 或 - count 小于 0。
        //
        //   T:System.ArgumentException:
        //     index 和 count 未指定 System.Collections.Generic.List`1 中的有效范围。 - 或 - 在排序过程中,comparer
        //     的实现会导致错误。例如,将某个项与其自身进行比较时,comparer 可能不返回 0。
        //
        //   T:System.InvalidOperationException:
        //     comparer 为 null,且默认比较器 System.Collections.Generic.Comparer`1.Default 找不到 T 类型的
        //     System.IComparable`1 泛型接口或 System.IComparable 接口的实现。
        public void Sort(int index, int count, IComparer<T> comparer);


        //
        // 摘要:
        //     使用指定的 System.Comparison`1 对整个 System.Collections.Generic.List`1 中的元素进行排序。
        //
        // 参数:
        //   comparison:
        //     比较元素时要使用的 System.Comparison`1。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     comparison 为 null。
        //
        //   T:System.ArgumentException:
        //     在排序过程中,comparison 的实现会导致错误。例如,将某个项与其自身进行比较时,comparison 可能不返回 0。
        public void Sort(Comparison<T> comparison);


        //
        // 摘要:
        //     使用默认比较器对整个 System.Collections.Generic.List`1 中的元素进行排序。
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     默认比较器 System.Collections.Generic.Comparer`1.Default 找不到 T 类型的 System.IComparable`1
        //     泛型接口或 System.IComparable 接口的实现。
        public void Sort();


        //
        // 摘要:
        //     使用指定的比较器对整个 System.Collections.Generic.List`1 中的元素进行排序。
        //
        // 参数:
        //   comparer:
        //     比较元素时要使用的 System.Collections.Generic.IComparer`1 实现,或者为 null,表示使用默认比较器 System.Collections.Generic.Comparer`1.Default。
        //
        // 异常:
        //   T:System.InvalidOperationException:
        //     comparer 为 null,且默认比较器 System.Collections.Generic.Comparer`1.Default 找不到 T 类型的
        //     System.IComparable`1 泛型接口或 System.IComparable 接口的实现。
        //
        //   T:System.ArgumentException:
        //     在排序过程中,comparer 的实现会导致错误。例如,将某个项与其自身进行比较时,comparer 可能不返回 0。
        public void Sort(IComparer<T> comparer);

3.举例说明

3.1.默认排序Sort()

自定义的类要实现IComparable接口,最好用泛型<T>,要不然有拆箱操作

自带数据类型可以直接使用Sort();

举例写自定义薪水类,有名子、年龄、薪水值字段,更加指定字段排序 

using System.Collections.Generic;
using System;

[Serializable]
public class Salary : IComparable<Salary>
{
    public string name;
    public int age;
    public int salaryNum;
    public Salary(string _name, int _age, int _salary)
    {
        name = _name;
        age = _age;
        salaryNum = _salary;
    }

    //Sort()默认调用方法
    //实现泛型接口:IComparable<Salary>
    public int CompareTo(Salary other)
    {
        return salaryNum.CompareTo(other.salaryNum);
    }

    //实现接口:IComparable,不建议使用
    //public int CompareTo(object obj)
    //{
    //    return salaryNum.CompareTo((obj as Salary).salaryNum);
    //}
}

调用list.Sort(),默认调用自定义类CompareTo()方法

    public List<Salary> list;
    void Start()
    {
        list = new List<Salary>()
        {
            new Salary("1 Jim",15,1000),
            new Salary("2 Jack",10,2000),
            new Salary("3 Tom",20,800),
            new Salary("4 Luccy",40,4000),
        };
        list.Sort();
    }

3.2Sort(IComparer<T> comparer)

以下几种方法默认类,并初始化

using System.Collections.Generic;
using System;
[Serializable]
public class Salary
{
    public string name;
    public int age;
    public int salaryNum;
    public Salary(string _name, int _age, int _salary)
    {
        name = _name;
        age = _age;
        salaryNum = _salary;
    }
}

 初始化,赋值

    public List<Salary> list;
    void Start()
    {
        list = new List<Salary>()
        {
            new Salary("1 Jim",15,1000),
            new Salary("2 Jack",10,2000),
            new Salary("3 Tom",20,800),
            new Salary("4 Luccy",40,4000),
        };
    }

正式开始Sort(IComparer<T> comparer)方法,自定义比较器

//自定义年龄比较器
public class AgeComparor : IComparer<Salary>
{
    public int Compare(Salary x, Salary y)
    {
        return x.age.CompareTo(y.age);
    }
}

调用

list.Sort(new AgeComparor());

3.3Sort(Comparison<T> comparison)

该方法不用先定义比较器

//根据名字排序         
list.Sort((a, b) => { return a.name.CompareTo(b.name); });

//或者更加薪水值排序
//list.Sort((a, b) => { return a.salaryNum.CompareTo(b.salaryNum); });

3.4Sort(int index, int count, IComparer<T> comparer)

要先定义 AgeComparor();

list.Sort(1, 2, new AgeComparor());

4.总结

个人认为,第三种方法比较简洁,自定义类不用集成比较接口,也不用自定义比较器,在使用时根据自己的需求自定义排序方式,但要注意注意结构,里面是有两个参数的拉姆达表达式

list.Sort((a, b) => { return a.name.CompareTo(b.name); });

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack Yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值