IComparer<T>、IComparable<T>、 Comparer<T> 类

ICompare<T>

定义:实现两个T类型对象的比较

实现方法: int Compare(T x, T y);

返回值:int 值,该值指示一个对象小于、等于还是大于另一个对象。

            当返回值<0时,默认x<y;

                          =0时,默认x=y;

                          >0时,默认x>y;

注意:前者是固定的,但后者是不固定的,所以可以用来更改排序的顺序

用法:

此接口与 List<T>.SortList<T>.BinarySearch 方法一起使用。 它提供了一种方法来自定义集合的排序顺序。 实现此接口的类包括 SortedDictionary<TKey,TValue>SortedList<TKey,TValue> 的泛型类。

此接口的默认实现是 Comparer<T>也就是该类继承了ICompare<T>接口StringComparer 类实现此接口,用来进行 String比较。

此接口支持顺序比较。 也就是说,当 Compare 方法返回0时,表示两个对象排序相同。 完全相等比较的实现由 IEqualityComparer<T> 泛型接口提供。

建议从 Comparer<T> 类(密封类)派生,而不是实现 IComparer<T> 接口,因为 Comparer<T> 类提供 IComparer.Compare 方法的显式接口实现,而不用自己实现,和获取对象的默认比较器的 Default 属性。

实例:

   public struct com : IComparer<int>//定义一个结构体或者类继承该接口
    {
        public int Compare(int x, int y)
        {
            if (x > y)
                return -1;//默认是x>y返回的是1,这里可以更改,让y>x返回1,也就是第二个数大于第一个数,从而达到逆序排列
            else if (x < y)
                return 1;
            else
                return 0;
        }

    }
   public class Android : MonoBehaviour
    {
        com coms = new com();
        //Comparer
        List<int> sorts = new List<int> { 1, 3, 2, 4, 6, 5 };
        void Start()
        {          
            sorts.Sort(new com());//用自定义的的排序方法
            //sorts.Sort();//用默认的排序方法,从小到大
            for (int i = 0; i < sorts.Count; i++)
            {
                Debug.Log(sorts[i]);
            }      
          }
     }

也可以用来进行根据多个属性进行排序,比如一个对象列表,每一个对象都有ABC三个属性,现根据A属性排序,A相同的根据B属性排序,B相同的根据C属性排序

实例:

public class Box : IComparable<Box>
{

    public Box(int h, int l, int w)
    {
        this.Height = h;
        this.Length = l;
        this.Width = w;
    }
    public int Height { get; private set; }
    public int Length { get; private set; }
    public int Width { get; private set; }
}

public class BoxLengthFirst : Comparer<Box> 
{
    public override int Compare(Box x, Box y)
    {
        if (x.Length.CompareTo(y.Length) != 0)
        {
            return x.Length.CompareTo(y.Length);
        }
        else if (x.Height.CompareTo(y.Height) != 0)
        {
            return x.Height.CompareTo(y.Height);
        }
        else if (x.Width.CompareTo(y.Width) != 0)
        {
            return x.Width.CompareTo(y.Width);
        }
        else
        {
            return 0;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Box> Boxes = new List<Box>();
        Boxes.Add(new Box(4, 20, 14));
        Boxes.Add(new Box(12, 12, 12));
        Boxes.Add(new Box(8, 20, 10));
        Boxes.Add(new Box(6, 10, 2));
        Boxes.Add(new Box(2, 8, 4));
        Boxes.Add(new Box(2, 6, 8));
        Boxes.Add(new Box(4, 12, 20));
        Boxes.Add(new Box(18, 10, 4));
        Boxes.Add(new Box(24, 4, 18));
        Boxes.Add(new Box(10, 4, 16));
        Boxes.Add(new Box(10, 2, 10));
        Boxes.Add(new Box(6, 18, 2));
        Boxes.Add(new Box(8, 12, 4));
        Boxes.Add(new Box(12, 10, 8));
        Boxes.Add(new Box(14, 6, 6));
        Boxes.Add(new Box(16, 6, 16));
        Boxes.Add(new Box(2, 8, 12));
        Boxes.Add(new Box(4, 24, 8));
        Boxes.Add(new Box(8, 6, 20));
        Boxes.Add(new Box(18, 18, 12));

        // Sort by an Comparer<T> implementation that sorts
        // first by the length.
        Boxes.Sort(new BoxLengthFirst());

        Console.WriteLine("H - L - W");
        Console.WriteLine("==========");
        foreach (Box bx in Boxes)
        {
            Console.WriteLine("{0}\t{1}\t{2}",
                bx.Height.ToString(), bx.Length.ToString(), 
                bx.Width.ToString());
        }
    }
}

IComparable<T>

定义:用来表示实现该接口的对象,与T类型的对象相比较

实现方法:int CompareTo(T other);

返回值:int 值,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同,或者是二者的大小,和    ICompare<T>一样

实例:

    public struct com : IComparable<int>
    {
        public int test;
        public int CompareTo(int other)
        {
            return test.CompareTo(other);
        }
    }
    public class Android : MonoBehaviour
    {
        com coms = new com();
       
        void Start()
        {
            coms.test = 3;
            Debug.Log(coms.CompareTo(5));     
         }
     }

 Comparer<T> 类

定义:抽象类,继承于ICompare<T>接口,实现了compare方法,是泛型 Comparer<T> 类,不是 Comparer 类

特性:包含有default,采用默认的compare方法

实例:

public class BoxLengthFirst : Comparer<Box> 
{
    // Compares by Length, Height, and Width.
    public override int Compare(Box x, Box y)
    {
        if (x.Length.CompareTo(y.Length) != 0)
        {
            return x.Length.CompareTo(y.Length);
        }
        else if (x.Height.CompareTo(y.Height) != 0)
        {
            return x.Height.CompareTo(y.Height);
        }
        else if (x.Width.CompareTo(y.Width) != 0)
        {
            return x.Width.CompareTo(y.Width);
        }
        else
        {
            return 0;
        }
    }
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TO_ZRG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值