Comparator based on different nullable fields of an object
I have an Employee object which contains two fields name and jobTitle . For sorting the employee objects, first priority should be on jobTitle , if jobTitle is null then the sorting should be based on name. Below is the Employee object public class Employee { private String name; private String jobTitle; } I used chained Comparator with JobTitlecomparator and NameComparator to achieve this: public class EmployeeChainedComparator implements Comparator<Employee> { private List<Comparator<Employee>> listComparators; @SafeVarargs public EmployeeChainedComparator(Comparator<Employee>... comparators