Using IComparer for sorting

前端 未结 4 680
南方客
南方客 2020-11-28 10:31

I am trying to use an IComparer to sort a list of Points. Here is the IComparer class:

public class CoordinatesBasedComparer : IComparer
{
    p         


        
4条回答
  •  时光说笑
    2020-11-28 11:06

    public class CoordinatesBasedComparer : IComparer, IComparer
    {
        public int Compare(Point a, Point b)
        {
            if ((a.x == b.x) && (a.y == b.y))
                return 0;
            if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)))
                return -1;
    
            return 1;
        }
        int IComparer.Compare(Object q, Object r)
        {
            return Compare((Point)q, (Point)r);            
        }
    }
    

提交回复
热议问题