Trying to create a priority queue with string length as the sorting factor, but the comparator doesn't seem to work. If I use the same operator say on a SortedSet, this works good.
the below code prints: [z, CD, ZHE21, CDE2312, ABC1]
whereas I was expecting: [z, CD, ABC1,ZHE21, CDE2312]
What am I missing?
PriorityQueue<String> queue = new PriorityQueue<String>(100, new Comparator<String>() {
public int compare(String a, String b) {
return Integer.compare(a.length(), b.length());
}
});
queue.add("CDE2312");
queue.add("ABC1");
queue.add("ZHE21");
queue.add("CD");
queue.add("z");
System.out.println(queue);