Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PriorityQueue returning incorrect ordering for custom comparator?
- public class PriQueTest {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- Comparator<String> comparator = new StringPriComparator();
- PriorityQueue<String> queue =
- new PriorityQueue<String>(3, comparator);
- queue.add("3"+"128763711");
- queue.add("2"+"128763712");
- queue.add("0"+"128763718");
- queue.add("1"+"128763713");
- queue.add("3"+"128763714");
- queue.add("2"+"128763715");
- queue.add("2"+"128763716");
- queue.add("3"+"128763717");
- while (queue.size() != 0)
- {
- System.out.println( (queue.remove().substring(1)));
- }
- }
- public static class StringPriComparator implements Comparator<String>
- {
- @Override
- public int compare(String x, String y)
- {
- if (Integer.parseInt(x.substring(0, 1)) > Integer.parseInt(y.substring(0, 1)))
- {
- return -1;
- }
- if (Integer.parseInt(x.substring(0, 1)) < Integer.parseInt(y.substring(0, 1)))
- {
- return 1;
- }
- return 0;
- }
- }
- }
- 128763711
- 128763714
- 128763717
- 128763716
- 128763712
- 128763715
- 128763713
- 128763718
- 128763711
- 128763714
- 128763717
- 128763712*
- 128763715*
- 128763716*
- 128763713
- 128763718
- public static class StringPriComparator implements Comparator<String> {
- @Override
- public int compare(String x, String y) {
- if (Integer.parseInt(x.substring(0, 1)) > Integer.parseInt(y.substring(0, 1))) {
- return -1;
- }
- if (Integer.parseInt(x.substring(0, 1)) < Integer.parseInt(y.substring(0, 1))) {
- return 1;
- }
- return x.substring(1).compareTo(y.substring(1));
- }
- }
Add Comment
Please, Sign In to add comment