I want to sort objects based on booleans, and I want to sort true values before false values.
Which of these compareTo implementations is more readable?
Using -1 to change the default behavior
public class Example implements Comparable<Example>{
Boolean isOk;
public int compareTo(Example o) {
return -1 * this.isOk.compareTo(o.isOk);
}
}
or swap sides of the Boolean # compareTo method?
public class ExampleTwo implements Comparable<ExampleTwo>{
Boolean isOk;
public int compareTo(ExampleTwo o) {
return o.isOk.compareTo(this.isOk);
}
}
source
share