zoukankan      html  css  js  c++  java
  • C# List.Sort与IComparer接口及Comparison委托应用于集合排序

    C#里List.Sort的用法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
      
    namespace ListSort
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<C> L = new List<C>();
                L.Add(new C { n = 1, s = "b" });
                L.Add(new C { n = 3, s = "a" });
                L.Add(new C { n = 2, s = "c" });<br>
                // 方法1 使用Comparison<T>委托。
                L.Sort((left, right) =>
                {
                    if (left.n > right.n)
                        return 1;
                    else if (left.n == right.n)
                        return 0;
                    else
                        return -1;
                });
      
                // 方法2 使用IComparer<T>接口。
                L.Sort(new CComparer());
      
                // 方法3 除以上两种方法以外还可以使用另一种方法,在C类中实现IComparable<T>
                L.Sort();
            }
        }
        //方法二
        public class CComparer : IComparer<C>
        {
            public int Compare(C left, C right)
            {
                if (left.n > right.n)
                    return 1;
                else if (left.n == right.n)
                    return 0;
                else
                    return -1;
            }
        }
        //方法三
        public class C : IComparable<C>
        {
            public int n;
            public string s;
      
            public int CompareTo(C other)
            {
                if (this.n > other.n)
                    return 1;
                else if (this.n == other.n)
                    return 0;
                else
                    return -1;
            }
        }
    }

    IComparer接口及Comparison委托应用于集合排序

    需要对list进行排序,可以用list.Sort()方法。该方法有多个重载。

    (1)使用IComparer<T>接口

    可以为Sort传入IComparer<T>的实现类的实例对象,该接口为:

    public interface IComparer<in T>
    {
     
        //如果x小于y,则返回负数;x大于y,返回正数;等于则返回0
        int Compare(T x, T y); 
     
    }

    void Sort(IComparer<T> comparison);

    如果使用IComparer<T>实现类的实例对象,则需要实现定义好实现类,对于已经定义好的,则比较方便,否则相对麻烦一些。

    (2)使用Comparison<T>委托

    但是对于没有定义IComparer<T>的实现类的场合,可以使用更为方便的方式,即使用Comparison<T>委托作为参数。

    public delegate int Comparison<in T>(T x, T y);

    void Sort(Comparison<T> comparison);

    具体使用时,可以直接传入委托(或函数名称),也可以直接使用numda表达式。以下是使用Lambda表达式的方法代码:

    List<Student> list=new List<Student>();  //Student类中含有Age属性
    list.AddRange(....);  //添加数据 
     
    //以下对Student集合按照其Age属性从小到大排序
    list.Sort( (x, y) =>
                        {
                            if (x.Age < y.Age)
                            {
                                return -1;
                            }
                            else if (x.Age > y.Age)
                            {
                                return 1;
                            }
                            else
                                return 0;
                        }
                        );    
     
    //或者以下更加简单的写法
    list.sort((x,y)=>x.Age.CompareTo(y.Age));   

    https://www.cnblogs.com/talentzemin/p/3930368.html

    https://blog.csdn.net/jiuzaizuotian2014/article/details/82425521

  • 相关阅读:
    [leedcode 82] Remove Duplicates from Sorted List II
    [leedcode 83] Remove Duplicates from Sorted List
    [leedcode 81] Search in Rotated Sorted Array II
    [leedcode 80] Remove Duplicates from Sorted Array II
    [leedcode 79] Word Search
    2018 ICPC青岛-books(思维题)
    CodeForces 15A-Cottage Village(思维题)
    CodeForces 755A-PolandBall and Hypothesis(思维题)
    CodeForces
    UVA11624-Fire!(BFS)
  • 原文地址:https://www.cnblogs.com/wintertone/p/12021166.html
Copyright © 2011-2022 走看看