I am adding edges to a PriorityQueue but for some reason they do not get sorted by their value, leading to a faulty result later.
My edge class looks like this
class Edge implements Comparable<Edge>{
int value;
String dest;
String start;
public Edge(String start, String dest, int g) {
this.dest = dest;
value = g;
this.start = start;
}
@Override
public int compareTo(Edge o) {
int temp = value - o.value;
if (temp > 0) {
return 1;
}
if (temp < 0) {
return -1;
}
return 0;
}
Yet, when I run my code where I do addAll on a LinkedList belonging to the Node "Springfield, MO" to the PriorityQueue, the Edges get sorted in the wrong order as seen below, what is the issue?
queue.addAll(list.get(node));
I have tried to make a specific comparator class for Edge and using it as a parameter in the PriorityQueue but I still get the same result.