I am using a comparator to sort a list based on three properties. I want entries to be at the end of the list if any of the following conditions are true
isNullable == true
required == false
defaultValue != null
This stub works in most cases, but I was informed of an edge case were we get error Comparison method violates its general contract! How can I fix it?
public static Comparator<CodegenProperty> propertyComparatorByNotNullableRequiredNoDefault = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.isNullable == another.isNullable && one.required == another.required && (one.defaultValue == null) == (another.defaultValue == null))
return 0;
else if (!one.isNullable && one.required && one.defaultValue == null)
return -1;
else
return 1;
}
};
