I am trying to use a priority Queue and was trying to get the numbers arranged in the reverse order [descending orders].
I implemented a comparator, and used the reverse convention as compared to natural order hoping that I would get numbers in reverse order.
public static void main(String[] args)
{
PriorityQueue<Integer> Descending = new PriorityQueue<Integer>(10,stats.new
minComparator());
Descending.add(5);
Descending.add(2);
Descending.add(7);
while(Descending.size() > 0)
{
System.out.print(Descending.remove());
}
}
class minComparator implements Comparator<Integer>
{
@Override
public int compare(Integer int1, Integer int2)
{
if(int1.intValue() < int1.intValue())
return 1;
else if(int1.intValue() > int1.intValue())
return -1;
else
return 0;
}
}
Here is the output:
5 7 2
This is neither ascending nor descending !. Can someone please help me out.
Thanks!
You compare the same number
int1to itself instead ofint1andint2