0

I am trying to sort a bunch of numbers and have the following code:

//sort possible targets based on distance
                Collections.sort(allPossibleTargets, new Comparator<NPD2>() {
                    @Override
                    public int compare(NPD2 o1, NPD2 o2) {
                        if (o1 == null && o2 == null)
                            return 0;
                        if (o1 == null && o2 != null)
                            return 1;
                        if (o1 != null && o2 == null)
                            return -1;
                        return o1.getNpd().getDistance() < o2.getNpd().getDistance() ? -1 : 1;
                    }
                });

However, I get the above exception. Please help. Thanks.

Abhishek Dey Das
  • 587
  • 7
  • 23

1 Answers1

0

You're Comparator doesn't return 0 if both objects are equal.

See this post: Comparison Method violates its general contract in Java 7

Community
  • 1
  • 1
avojak
  • 2,342
  • 2
  • 26
  • 32