Answer
Sort List of Custom Objects in Java
Define the Employee class
Java
public class Employee {
private String name;
private int age;
private double salary;
private String department;
public Employee(String name, int age, double salary, String department) {
this.name = name;
this.age = age;
this.salary = salary;
this.department = department;
}
// Getters
public String getName() { return name; }
public int getAge() { return age; }
public double getSalary() { return salary; }
public String getDepartment() { return department; }
@Override
public String toString() {
return name + "(" + age + ", $" + salary + ", " + department + ")";
}
}
Method 1: Comparable — Natural Order (single criterion)
Java
// Make Employee implement Comparable to define natural ordering
public class Employee implements Comparable<Employee> {
// ... fields and getters ...
@Override
public int compareTo(Employee other) {
return this.name.compareTo(other.name); // natural order = by name
}
}
List<Employee> employees = Arrays.asList(
new Employee("Charlie", 30, 75000, "QA"),
new Employee("Alice", 25, 90000, "Dev"),
new Employee("Bob", 28, 80000, "QA")
);
Collections.sort(employees); // uses compareTo
employees.forEach(System.out::println);
// Alice(25, $90000.0, Dev)
// Bob(28, $80000.0, QA)
// Charlie(30, $75000.0, QA)
Method 2: Comparator — Custom Order (multiple criteria)
Java
// Sort by salary ascending
employees.sort(Comparator.comparingDouble(Employee::getSalary));
// Sort by salary descending
employees.sort(Comparator.comparingDouble(Employee::getSalary).reversed());
// Sort by age ascending
employees.sort(Comparator.comparingInt(Employee::getAge));
// Sort by department, then by name within department
employees.sort(
Comparator.comparing(Employee::getDepartment)
.thenComparing(Employee::getName)
);
// Sort by salary descending, then age ascending
employees.sort(
Comparator.comparingDouble(Employee::getSalary).reversed()
.thenComparingInt(Employee::getAge)
);
Method 3: Lambda Comparator (Inline)
Java
// Sort alphabetically by name
employees.sort((e1, e2) -> e1.getName().compareTo(e2.getName()));
// Sort by salary descending (manual)
employees.sort((e1, e2) -> Double.compare(e2.getSalary(), e1.getSalary()));
Method 4: Java 8 Stream Sorted
Java
// Returns NEW sorted list — doesn''t modify original
List<Employee> byName = employees.stream()
.sorted(Comparator.comparing(Employee::getName))
.collect(Collectors.toList());
// Find top 3 highest paid
List<Employee> top3 = employees.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
.limit(3)
.collect(Collectors.toList());
In Automation Testing
Java
// Sort test data before comparison
List<String> actualProducts = getProductNamesFromUI();
List<String> expectedProducts = Arrays.asList("Laptop", "Mouse", "Keyboard");
Collections.sort(actualProducts);
Collections.sort(expectedProducts);
assertEquals(expectedProducts, actualProducts, "Product list mismatch");
// Verify dropdown is sorted
List<String> dropdownOptions = getDropdownOptions();
List<String> sortedExpected = new ArrayList<>(dropdownOptions);
Collections.sort(sortedExpected);
assertEquals(sortedExpected, dropdownOptions, "Dropdown not sorted alphabetically");
