I've seen a lot of post on this problem and almost every one of them is because the code was not transitive and the original poster did not handle the case where object 1 && object 2 == null, but I have handled it in this case and I am still getting this error.
private static Comparator<MyObject> sortByDateComparator() { return (o1, o2) -> { if (o1 == null && o2 == null) { return 0; } if (o1 == null) { return 1; } if (o2 == null) { return -1; } if (TextUtils.isEmpty(o1.eventTime())) { if (TextUtils.isEmpty(o2.eventTime())) { return 0; } else { return 1; } } else if (TextUtils.isEmpty(o2.eventTime())) { return -1; } return o2.eventTime() .compareTo(o1.eventTime()); }; } public static ArrayList<MyObject> sortByDate(ArrayList<MyObject> obj) { Collections.sort(obj, ThisClass.<MyObject>sortByDateComparator()); return obj; }
The eventTime attibute is a simple string public String eventTime = DateUtil.getCustomDateClassInstance().getZeroUtcTime(); //init with epoch time
I sometimes get this crash but it is pretty rare.
Comparison method violates its general contract!
https://stackoverflow.com/questions/67393947/comparison-method-violates-its-general-contract-using-string-compareto-and-after May 05, 2021 at 09:04AM
没有评论:
发表评论