In Comparator, the arguments of Compare() are supposed to be swapped when 1 is returned, but i am seeing otherwise, what am I doing wrong? and also what is the order of the arguments?
import java.util.Comparator;
import java.util.List;
public class test2
{
// Defines edge structure
static class Edge
{
int src, dest, weight;
public Edge(int src, int dest, int weight)
{
this.src = src;
this.dest = dest;
this.weight = weight;
}
@Override
public String toString()
{
return "Edge [src=" + src + ", dest=" + dest + ", weight=" + weight + "]";
}
}
public static void main(String[] args)
{
int V = 4;
List<Edge> graphEdges = new ArrayList<Edge>(
List.of(new Edge(0, 1, 10), new Edge(0, 2, 6),
new Edge(0, 3, 5), new Edge(1, 3, 15),
new Edge(2, 3, 4)));
graphEdges.sort(new Comparator<Edge>()
{
@Override public int compare(Edge o1, Edge o2)
{
System.out.println(graphEdges);
System.out.println("o1="+o1.weight);
System.out.println("o2="+o2.weight);
return Integer.compare(o1.weight, o2.weight);
/*Integer w = o1.weight - o2.weight;
System.out.println(w);
System.out.println(graphEdges);
return o1.weight - o2.weight;*/
}
});
System.out.println(graphEdges);
}
}```