If you have a List of the employee objects then you need to use Collections.sort() method to sort the list holding employee objects. Below example will show how to write custom comparator. I’ve given three examples of custom comparators.
Employee Object
Next you need to define the custom comparator which will help you to sort by employee name, gender and age.
Name comparator
Gender Comparator
Age Comparator
If you apply NameComarator
The result will be sorted based on name.
If you apply AgeComparator
The result will be sorted by age.
If you apply GenderComparator
The result will be sorted by gender
No comments:
Post a Comment