0

so I'm trying to sort my arraylist of geometric objects in ascending order (by area). However, I'm running into an error

The method add(GeometricObject) in the type ArrayList<GeometricObject> is not applicable for the arguments (double)

in my code block

private static void selectionSort(ArrayList<GeometricObject> list) {
        for (int i = 0; i < list.size(); i++) {
            if (i < list.size() - 1) {
                if (list.get(i).getArea() > list.get(i + 1).getArea()) {
                    double j = list.get(i).getArea();
                    list.remove(i);
                    list.add(i, list.get(i));
                    list.remove(i + 1);
                    list.add(j).getArea();
                    i = -1;
                }
            }
        }

     }

line

list.add(j).getArea();

Again, I'm just trying to sort them in order of their areas so i can later print the array. Any help or pointers in the right direction would be appreciated, thanks!

6
  • Why are you adding a double to a ArrayList<GeometricObject>? Commented Oct 14, 2015 at 0:33
  • what exactly is the purpose of these lines: list.add(i, list.get(i)); and list.add(j).getArea();? doesn't make a lot of sense to me Commented Oct 14, 2015 at 0:36
  • Ahh I didn't even catch myself facepalm. I think i may have solved the error by setting j equal to a GeometricObject and removing the getArea method and doing the same for the list.add(j) line. @SotiriosDelimanolis Commented Oct 14, 2015 at 0:37
  • Doesn't really make sense to me either lol, I thought i was just swapping around the areas when in reality I'm supposed to be swapping the entire objects list order. @Paul Commented Oct 14, 2015 at 0:38
  • Why don't you want to sort by using standard java Collections.sort and Comparator? Commented Oct 14, 2015 at 0:45

2 Answers 2

1

I think you wanted to do this instead

private static void selectionSort(ArrayList<GeometricObject> list) {
        for (int i = 0; i < list.size(); i++) {
            if (i < list.size() - 1) {
                if (list.get(i).getArea() > list.get(i + 1).getArea()) {
                    GeometricObject j = list.get(i);
                    list.remove(i);
                    list.add(i, list.get(i));
                    list.remove(i + 1);
                    list.add(j);
                    i = -1;
                }
            }
        }

     }

I would consider change j to a more meaningful name. The issue occurred because of incomparable datatypes

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what i was aiming for. Thanks!
@RiFFRAFF you can make use of Collections.Sort() method to sort your ArrayList with Geometric objects rather than coding down selection sort and merge sort on your own. I have posted it here ! stackoverflow.com/questions/33116198/…
0

Try this:

    Collections.sort(list, new Comparator<GeometricObject>() {
        @Override
        public int compare(GeometricObject o1, GeometricObject o2) {
            if (o1.getGmtModified() != null && o2.getGmtModified() != null){
                if(o1. getArea() < o2. getArea()) {
                    return 1;
                }else if(o1. getArea() > o2. getArea()){
                    return -1;
                }else{
                    return 0;                        
                }
            }
            return 0;
        }
    });

Comments

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.