How can I improve the comparator for objects by several fields? The comparator must sort user by last name or first name if last name doesn't exist. And if there are no last name and first name sort by username. But these users must be at the end of the list.
public static Comparator<UserConfigurationDto> BY_LASTNAME = (u1, u2) -> {
// if users have only username compare them
if((u1.getLastName().isEmpty() && u1.getFirstName().isEmpty())
&& (u2.getLastName().isEmpty() && u2.getFirstName().isEmpty())){
return u1.getUsername().compareToIgnoreCase(u2.getUsername());
}
//if user doesnt have firstName and LastName drop them at the end
if(u1.getLastName().isEmpty() && u1.getFirstName().isEmpty()){
return 1000000 + getWeight(u1.getUsername());
}
if(u2.getLastName().isEmpty() && u2.getFirstName().isEmpty()){
return -1000000 + getWeight(u2.getUsername());
}
String s1 = u1.getLastName().isEmpty() ? u1.getFirstName() : u1.getLastName();
String s2 = u2.getLastName().isEmpty() ? u2.getFirstName() : u2.getLastName();
return s1.compareToIgnoreCase(s2);
};
}
private static int getWeight(String s){
return s.codePoints().sum();
}
Does anybody have an idea how to improve this? I try to use Comparator.comparing and Comparator.thenComparing but they produce an incorrect result