1

I have multiple objects set up, all implementing the same class. All of these objects have a common method, "getRatio". I want to order these objects in ascending numerical order with respect to the values of the "getRatio" method, and also have the objects call their toString methods in order. I've attempted to apply this idea, but I was only to order just the numbers themselves.

    List shapeList = new ArrayList();
    shapeList.add(rectangle);
    shapeList.add(triangle_right);
    shapeList.add(isosceles);
    shapeList.add(triangle);
    shapeList.add(triangle2);
    shapeList.add(triangle3);
    Collections.sort(shapeList);
    for (Shape shape : shapeList) {
        System.out.println(shape.toString());
    }

no suitable method found for add(RightTriangle) shapeList.add(triangle_right);

error: cannot find symbol Comparable.sort(shapeList);

1
  • You should change Comparable.sort(shapeList) to Collections.sort(shapeList). You should also use a type parameter when you declare shapeList... like this: List<Shape> shapeList = new ArrayList<Shape>();` Commented Nov 20, 2012 at 4:40

3 Answers 3

2

You can provide a Comparator for the Arrays.sort() method. In your case it would look sth like this (I assume the getRatio method is in a common Shape class/interface):

public class ShapeComparator implements Comparator<Shape> { 
    int compareTo (final Shape shape1, final Shape shape2) {
        return (int) Math.signum (shape1.getRatio () - shape2.getRatio ());
    }
}

You can also make your common class implement the Comparable interface,like this:

public class Shape implements Comparable<Shape> {
    int compareTo (final Shape other) {
        return (int) Math.signum (getRatio () - other.getRatio ());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Expanding on the other answers, you could define your Comparator and sort your array as follows:

Arrays.sort(myArray, new Comparator<MyClass>() {
    @Override
    public int compare(MyClass c1, MyClass c2) {
        return (new Double(c1.getRatio())).compareTo(c2.getRatio());
    }
});

If you plan sorting multiple arrays like this, it would be wise to make MyClass implement the Comparable interface.


EDIT: To sort Lists (such as ArrayLists) you can use a similar concept, but with Collections.sort:

Collections.sort(shapeList, new Comparator<MyClass>() {
    @Override
    public int compare(MyClass c1, MyClass c2) {
        return (new Double(c1.getRatio())).compareTo(c2.getRatio());
    }
});

Relevant documentation:

7 Comments

In my code I am getting an error for incompatible types for shapeList. But is that what I am looking for?
@ElmirKouliev What is shapeList?
@ElmirKouliev Is this an ArrayList (or other List) or just an array? i.e. how is the variable defined?
You should include a type parameter when you define your ArrayList. Otherwise it will be assumed that your list contains Objects. An Object is not the same as a Shape. You should declare your list like this: List<Shape> shapeList = new ArrayList<Shape>();
We can't read your mind. If you're getting errors and you'd like help, you'll have to share some details. Also, the point of this site is not for people to hold your hand while walking through every step of completing your homework assignment. If you've stumbled onto a new error, plese edit your question accordingly. Otherwise, consider creating a new question that is related to your new error. How do you expect us to respond to "I'm getting two errors" if that's all you tell us? We can't see the code you're typing!!
|
0

You should make your Object implement Comparable.

More info here

You'll want to implement compareTo() so it compares the ratio of the two objects.

Here's how you might do that:

class Foo implements Comparable<Foo> {

    private double ratio;

    public double getRatio() {
        return ratio;
    }

    public int compareTo(Foo otherFoo) {
        if (otherFoo == null) {
            return -1;
        }
        return ratio - otherFoo.ratio;
    }

}

Here's how you would sort a Collection of Foo objects:

List<Foo> fooList = createFooList();
Collections.sort(fooList);

// print the Foos in order

for (Foo f : fooList) {
    System.out.println(f.toString());
}

7 Comments

i have set up a compare to method, just not sure how to implement it into an array using objects
If your object implements Comparable, its compareTo method will automatically be used to sort Collections that are ordered. You can also sort a Collection by using Collections.sort(someCollection);
So then after they are ordered, how do I call the toString methods of these objects, in the order created by the Comparable
Iterate over your ordered collection and call toString() on each item in the Collection. Hint: use a for loop.
I was able to set up an array that applies the comparator
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.