Set of comparator error java.lang.IllegalArgumentException: Comparison method violates its general contract!

 

        The Collections.sort (listMonthlyUsage, new new Comparator <MonthlyUsageDto> () { // accordance elements in ascending order 
            @Override
             public  int Compare (MonthlyUsageDto O1, O2 MonthlyUsageDto) {
                 IF (o1.getCstId ()> o2.getCstId ())   return . 1 ;
                 the else  return -1 ;
 //                 return o1.getCstId () - o2.getCstId (); 
            } 
        });

The code above error because we comparator is not precise enough, and only returns 1 and -1, 0 meters have returned. The code is changed to normal following:

        The Collections.sort (listMonthlyUsage, new new Comparator <MonthlyUsageDto> () { // accordance elements in ascending order 
            @Override
             public  int Compare (MonthlyUsageDto O1, O2 MonthlyUsageDto) {
                 IF (o1.getCstId ()> o2.getCstId ())   return . 1 ;
                 the else  IF (o1.getCstId () == o2.getCstId ())   return 0 ;
                 the else  return -1 ;
 //                 return o1.getCstId () - o2.getCstId (); 
            } 
        });

 

Guess you like

Origin www.cnblogs.com/mike-mei/p/11223128.html