Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee s, Employee s1) {
int comp = s.getName().compareTo(s1.getName());
if (comp != 0) { // names are different
return comp;
}
return s.getSalary() - s1.getSalary();
}
});
System.out.println(employees);
}
Use BigDecimal.compareTo(BigDecimal) which already does all the logic for you:
return s.getSalary().compareTo(s1.getSalary());
The - (minus) operator is only defined for the primitive numbers and their dedicated wrapper type.
As the other mentionned, this is not the best way to handle the problem you showed, however, one can not use minus operator on BigDecimal. You need to use the substract method to avoid that error.
//s.getSalary() - s1.getSalary();
BigDecimal diff = s.subtract(s1);
You can then cast that diff to int using .intValue(). Be careful thoug, as it may produce side effects because of the information loss from BidDecimal to int...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With