Jump to content
  • Advertisement
Sign in to follow this  
vegi

[.net] IComparer

This topic is 3237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i´m using an IComparer to Sort my Pointlist. I want to sort it by increasing x and then increasing y-coordinates But it doesn´t work :(
public class PointComparer : IComparer
    {
        private bool coord;
        //
        public PointComparer(bool coord)
        {
            this.coord = coord;
        }
        //
        int IComparer.Compare(object a, object b)
        {
            var b1 = (Point)a;
            var b2 = (Point)b;
            if(coord)
            {
                if (b1.X < b2.X)
                    return -1;
                if (b1.X > b2.X)
                    return 1;
                return 0;
            }
            else
            {
                if (b1.X == b2.X)
                {
                    if (b1.Y < b2.Y)
                        return -1;
                    if (b1.Y > b2.Y)
                        return 1;
                    return 0;
                }
                return 0;
            }
        }
    }
//...
PVRTXList.Sort(new PointComparer(true)); // to sort by x
PVRTXList.Sort(new PointComparer(false)); // to sort the same x by y

Share this post


Link to post
Share on other sites
Advertisement
You should perhaps try sorting using one comparer, instead of doing it in two steps, which is just awkward.


public class PointComparer : IComparer<Point>
{
public int Compare(Point a, Point b)
{
if ( a.x < b.x ) return -1;
else if ( a.x > b.x ) return 1;
else return a.y - b.y;
}
}



I haven't tested it, but it should work. I you really need a non-generic version, just add the necessary casts. Also, it might be safer to replace the last line by a couple of extra ifs, as you might have some overflow issues there.

Share this post


Link to post
Share on other sites
Nope, this doesn´t work.


i also found out that i can sort with

Comparer.Default.Compare(b1.X, b2.X);

but this doesn´t really helps : /

Share this post


Link to post
Share on other sites

public class App
{
public static void Main( string[] args )
{
var pts = new List<Point>();

pts.Add( new Point( 1, 1 ) );
pts.Add( new Point( 2, 1 ) );
pts.Add( new Point( 1, 2 ) );
pts.Add( new Point( 2, 2 ) );

pts.Sort( new PointComparer() );

foreach ( var p in pts )
{
Console.WriteLine( p );
}
}
}

public class Point
{
public Point( float x, float y )
{
this.X = x;
this.Y = y;
}

public float X { get; set; }

public float Y { get; set; }

public override string ToString()
{
return string.Format( "({0}, {1})", X, Y );
}
}

public class PointComparer : IComparer<Point>
{
public int Compare( Point a, Point b )
{
var cx = a.X.CompareTo( b.X );

if ( cx != 0 )
{
return cx;
}
else
{
return a.Y.CompareTo( b.Y );
}
}
}


Seems to work:

(1, 1)
(1, 2)
(2, 1)
(2, 2)

Share this post


Link to post
Share on other sites
You're making the assumptions that the second sort knows it has to leave the "same X island" unperturbed, which I think is the case if the algorithm is stable (i.e. the relative position of equal elements must be preserved), but which it is not according to MSDN.

So, the algorithm you use probably just guarantees that a) the resulting list will be a permutation of the original one, and b) the elements will be ordered with respect to the predicate you provided. This means that the list {(1,2), (1,1), (2,2), (2,3), (3,3), (3,1)} can be sorted to many different "valid" lists according to your predicate (426 different possibilities if I'm not mistaken). My version of the predicate only allows for one correct solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!