List排序 Sort的用法

    技术2022-07-15  75

    目录

    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); });

    Processed: 0.011, SQL: 9