I want to write a program which can sort Items by Priority Queue
But the code below doesn't work and I dont know the problem
any help?? thanks in advance for your attention
public class SortingASequenceByPriorityQueue {
public static void main(String[] args) {
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(1000, new Comparator<Integer>() {
public int compare(Integer w1, Integer w2) {
if(w1.intValue()> w2.intValue())
return 1;
else if( w1.intValue()< w2.intValue())
return -1;
return 0;
}
});
pQueue.add(12);
pQueue.add(1);
pQueue.add(5);
pQueue.add(22);
pQueue.add(3);
pQueue.add(2);
pQueue.add(124);
pQueue.add(14);
pQueue.add(111);
pQueue.add(9);
pQueue.add(30);
Object[] ar = pQueue.toArray();
for (int i = 0; i< ar.length; i++){
System.out.println(ar[i].toString());
}
}
}
The output is this :
1 3 2 14 9 5 124 22 111 12 30