1

What i am trying to do :

Sort the list of objects based on distance using comparator


private Point getNearestPixelValue(int pixlX, int pixlY) {

        LinkedList<DistancePoint> mList=new LinkedList<DistancePoint>();

        for (int i = 0; i < modelModFile.getStroke_paths().size(); i++) {
            for (int j = 0; j < modelModFile.getStroke_paths().get(i).getMain_stroke().size(); j++) {

                if (j < modelModFile.getStroke_paths().get(i).getMain_stroke().size() - 1) {
                    String a = modelModFile.getStroke_paths().get(i).getMain_stroke().get(j).getStroke();
                    String[] fA = a.split(",");
                    Point p1 = new Point(Integer.valueOf(fA[0]), Integer.valueOf(fA[1]));
                    Point p2 = new Point(pixlX, pixlY);
                    //Add the calculated distance to a integerArray
                    DistancePoint mObj=new DistancePoint(p2,calDistBtwTwoPixels(p1, p2));
                    mList.add(mObj);
                }
            }
        }

        //Sort the array
        Arrays.sort(mList, DistancePoint.distanceComparator);

        //Return the nearest point
        return mList.get(0).getmPoint();
    }

DistancePoint.java

public class DistancePoint implements Comparable<DistancePoint>{

    int mDistance;
    Point mPoint;

    public DistancePoint(Point mPoint, int mDistance) {
        super();
        this.mPoint = mPoint;
        this.mDistance = mDistance;
    }


    public int getmDistance() {
        return mDistance;
    }

    public void setmDistance(int mDistance) {
        this.mDistance = mDistance;
    }

    public Point getmPoint() {
        return mPoint;
    }

    public void setmPoint(Point mPoint) {
        this.mPoint = mPoint;
    }


    public static Comparator<DistancePoint> distanceComparator = new Comparator<DistancePoint>() {

        @Override
        public int compare(DistancePoint e1, DistancePoint e2) {
            return (int) (e1.getmDistance() - e2.getmDistance());
        }
    };


    @Override
    public int compareTo(DistancePoint distancePoint) {
        //let's sort the employee based on distance in ascending order.
        return (this.mDistance - distancePoint.mDistance);
    }
}

Error i am getting:

enter image description here

How to resolve this, Not able to understand the error. am i using the comparator incorrectly

1
  • 1
    You use Arrays.sort but a LinkedList is not an array. Use Collections.sort instead. If if you want to return the nearest point, you don't need to sort it. While traversing the list, update the point as long as you find one that is nearer. Commented Nov 15, 2015 at 2:22

1 Answer 1

1

You are trying to sort a List<T>; what you should use is:

Collections.sort(theList, theComparator);

Arrays.sort() expects a T[] (an array of T) which is not quite the same thing -- in more ways than one.

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

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.