Comparison method violates its general contract [How to Solve]

Error code:

//Error Codes
        Comparator<InspectReportListItemMpVo> comparator = new Comparator<InspectReportListItemMpVo>() {
            @Override
            public int compare(InspectReportListItemMpVo o1, InspectReportListItemMpVo o2) {
                try {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date reportTime_o1 = simpleDateFormat.parse(o1.getReportTime());
                    Date reportTime_o2 = simpleDateFormat.parse(o2.getReportTime());
//                    return reportTime_o1.compareTo(reportTime_o2);
                    return reportTime_o2.compareTo(reportTime_o1);
                } catch (ParseException e) {
                    e.printStackTrace();
                    return 1;
                }
            }
        };
        result.getItems().sort(comparator);

Screenshot of error reporting:

Comparison method violates its general contract

Problem-solving:

(not rigorous!) return 1 in the catch block; Change to return 0;

Supplement: rigorous writing

       Collections.sort(list, new Comparator<String>() {
            public int compare(String arg1, String arg2) {
                /**
                 * Modified
                 */
                if(StringUtils.isBlank(arg1) ) {
                    if(StringUtils.isBlank(arg2)) {
                        return 0;
                    }else {
                        return -1;
                    }
                    
                }else if(StringUtils.isBlank(arg2)){
                    return 1;
                }
                
                return arg1.compareTo(arg2);
            }
        });

Cause: timsort has a reflexivity check

Timport algorithm rules:
after JDK7, after implementing the comparable interface, the following three characteristics should be met:
1) Reflexivity: the comparison results of X and y are opposite to those of Y and X
2) transitivity: X > y,y> z. Then x > z。
3) symmetry: if x = y, the comparison results of X and Z are the same as those of Y and Z.

For the above error code, if you directly return 1, assume that a is empty, a.compareto (b) = 1, and b.compareto (a) = 1, which does not satisfy reflexivity

Read More: