Question

If a rectangle has the same x value then compare the y value, then check the width and height and so on.

public class RectangleTester
{
    public static void main(String[] args)
    {
        ArrayList<Rectangle2D> list = new ArrayList<>();
        list.add(new Rectangle2D.Double(100,100,50,50));
        list.add(new Rectangle2D.Double(120,80,54,75));
        list.add(new Rectangle2D.Double(85,95,60,55));
        list.add(new Rectangle2D.Double(100,95,55,55));
        list.add(new Rectangle2D.Double(100,95,55,52));

        System.out.println("\nUnsorted Rectangles:");
        for (Rectangle2D temp : list)
            System.out.println(temp.toString() + " Y: " + temp.getY());

This is where I get my logic all twisted up. I don not know why the comparator is not correctly listing the correct order when comparing 2 rectangles with the same x-value or y-value.

Comparator<Rectangle2D> comp = new
            Comparator<Rectangle2D>()
            {
                public int compare(Rectangle2D r1, Rectangle2D r2)
                {
                    Double r1X = r1.getX();
                    Double r2X = r2.getX();
                    Double r1Y = r1.getY();
                    Double r2Y = r2.getY();
                    Double r1Width = r1.getWidth();
                    Double r2Width = r2.getWidth();
                    Double r1Height = r1.getHeight();
                    Double r2Height = r2.getHeight();

                    if (r1X == r2X && r1Y == r2Y && r1Width == r2Width && r1Height != r2Height)
                        return r1Height.compareTo(r2Height);
                    else if (r1X == r2X && r1Y == r2Y && r1Width != r2Width)
                        return r1Width.compareTo(r2Width);
                    else if (r1X == r2X && r1Y != r2Y)
                        return r1Y.compareTo(r2Y);
                    else
                        return r1X.compareTo(r2X);
                }
            };

        Collections.sort(list, comp);

        System.out.println("\nSorted Rectangles:");
        for (Rectangle2D temp : list)
            System.out.println(temp.toString());
    }
}

My output shows like this:

Unsorted Rectangles:

java.awt.geom.Rectangle2D$Double[x=100.0,y=100.0,w=50.0,h=50.0] Y: 100.0
java.awt.geom.Rectangle2D$Double[x=120.0,y=80.0,w=54.0,h=75.0] Y: 80.0
java.awt.geom.Rectangle2D$Double[x=85.0,y=95.0,w=60.0,h=55.0] Y: 95.0
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=55.0] Y: 95.0
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=52.0] Y: 95.0

Sorted Rectangles:

java.awt.geom.Rectangle2D$Double[x=85.0,y=95.0,w=60.0,h=55.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=100.0,w=50.0,h=50.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=55.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=52.0]
java.awt.geom.Rectangle2D$Double[x=120.0,y=80.0,w=54.0,h=75.0]

As you can see, the rectangles are sorted correctly with the x values but when multiple rectangles have the same x value the sort fails to place them in the right order. Im pulling my hair out with the logic on my if statements I just need another set of eyes.

Was it helpful?

Solution

First error, you're comparing objects with ==. == compares references, not values.

Second error, your logic is incorrect, and unnecessary complex. Just implement it as you described it: If a rectangle has the same x value then compare the y value:

int result = r1X.compareTo(r2X);
if (result == 0) {
    result = r1Y.compareTo(r2Y);
}
if (result == 0) {
    result = r1Width.compareTo(r2Width);
}
if (result == 0) {
    result = r1Height.compareTo(r2Height);
}
return result;

With Guava, it would be easily implemented with

return ComparisonChain.start()
                      .compare(r1X, r2X)
                      .compare(r1Y, r2Y)
                      .compare(r1Width, r2Width)
                      .compare(r1Height, r2Height)
                      .result();

With Java 8, it would be even cleaner:

Comparator<Rectangle> comparator = 
    Comparator.comparing(Rectangle::getX)
              .thenComparing(Rectangle::getY)
              .thenComparing(Rectangle::getWidth)
              .thenComparing(Rectangle::getHeight)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top