NWcis
NWcis

Reputation: 59

Collections.sort not working after overriding compare and compareTo

The issue I'm having is using Collections.sort(linkedList); to try and sort a linked list full of points. I have already modified the compare and compareTo methods to fit the needs. as posted here, This snipped is for comparing the Y values in the list that way we can sort them.

 package points;

 import java.util.Comparator;

 public class CompareY implements Comparator<Point>
 {
    public int compare(Point p1, Point p2)
    {
         int equals = 0;

    if(p1.getY() > p2.getY())
    {
        equals = 1;
    }
    else if(p1.getY()< p2.getY())
    {
        equals = -1;
    }
    else if(p1.getY() == p2.getY())
    {
        //If the 'Y's' are equal, then check the 'X's'
        if(p1.getX() > p2.getX())
        {
            equals = 1;
        }
        if(p1.getX() < p2.getX())
        {
            equals = -1;
        }
    }
    return equals;
  }
}

My comparable (compare) method is within the main class, Point as shown here:

package points;

public class Point implements Comparable<Point>
{
    int x;
    int y;

public Point()
{
    //Blank default constructor
}
public Point(int x, int y)
{
    this.x = x;
    this.y = y;
}


//Auto generated getters and setters
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}

public int compareTo(Point o)
{
    int equals = 0;

    if(this.getX() > o.getX())
    {
        equals = 1;
    }
    else if(this.getX() < o.getX())
    {
        equals = -1;
    }
    else if(this.getX() == o.getX())
    {
        //If the 'X's' are equal, then check the 'Y's'
        if(this.getY()> o.getY())
        {
            equals = 1;
        }
        if(this.getY() < o.getY())
        {
            equals = -1;
        }
    }
    return equals;
  }

}

My issue lays within the Test class, where I try to call

Collections.sort((List<Point>) linkedList);

I get the error "The method sort(List) in the type Collections is not applicable for the arguments (List<Point>"

Which I don't understand where it's coming from or why it is there if I did my methods.

TEST CODE:

package points;
import java.util.*;
import java.awt.Point;
public class Test
{

public static void main(String[] args) 
{
    Random rand = new Random();
    int sizeLimit = 10;

    LinkedList<Point> linkedList = new LinkedList<Point>();

    //Populating our linkedList with random values.
    for(int i=0; i < sizeLimit; i++)
    {
        linkedList.add(new Point(rand.nextInt(10), rand.nextInt(10)));
    }

    System.out.println("original list");

    //Declaring our iterator to step through and print out our elements
    Iterator<Point> iter = linkedList.iterator();
    while(iter.hasNext())
    {
        System.out.println(iter.next());
    }

    Collections.sort((List<Point>) linkedList);     

    }
}

Upvotes: 0

Views: 289

Answers (2)

Thilo
Thilo

Reputation: 262524

import java.awt.Point;

You are importing the wrong Point class. The code does not use yours now.

If you remove that line, you will get the Point class from the current package.

Or you could do

import points.Point;

Upvotes: 3

Stephen Neal
Stephen Neal

Reputation: 56

As mentioned in one of the comments the cast to List in the sort call is redundant. It sounds like you are using a List class other than java.util.List. Check your imports.

Failing that you should post your test code.

Upvotes: 1

Related Questions