Sort a 2D Points List (first by X and then Y)

Posted by Mikos on Stack Overflow See other posts from Stack Overflow or by Mikos
Published on 2010-05-08T15:04:38Z Indexed on 2010/05/08 15:08 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

I am trying to sort a List of 2D Points first by x co-ordinate and then by y co-ordinate. I implemented the IComparer interface as follows:

class PointComparer : IComparer<Point>
{
    public int Compare(Point x, Point y)
    {
        if (x.Y != y.Y)
        {
            return x.Y - y.Y;
        }
        else
        {
            return x.X - y.X;
        }

    }
}

And then call my sorting as follows:

pointsList.Sort(new PointComparer());

For some reason the list doesn't sort. Surely is something very simple and silly, but stuck on this for quite a while....TIA

© Stack Overflow or respective owner

Related posts about c#

Related posts about sorting