2015-03-31 63 views
0

我試圖理清一堆數字和有下面的代碼:」java.lang.IllegalArgumentException:比較方法違反其通用合約!「錯誤的Collections.sort()

//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; 
        } 
       }); 

不過,我得到上述例外。請幫忙。謝謝。

回答

相關問題