You
can sort list in java using Comparator and Collections class. I am going to
explain with following example. Following example Uses List of Employees and
sort them by using firstName, lastName and id.
public class Employee { private int id; private String firstName; private String lastName; Employee(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Employee{" + "id=" + id + "firstName=" + firstName + "lastName=" + lastName + '}'; } }
import java.util.Comparator; public class EmployeeIdComparator implements Comparator<Employee> { public int compare(Employee emp1, Employee emp2) { return emp1.getId() - emp2.getId(); } }
import java.util.Comparator; public class EmployeeFirstNameComparator implements Comparator<Employee> { public int compare(Employee emp1, Employee emp2) { return emp1.getFirstName().compareTo(emp2.getFirstName()); } }
import java.util.Comparator; public class EmployeeLastNameComparator implements Comparator<Employee> { public int compare(Employee emp1, Employee emp2) { return emp1.getLastName().compareTo(emp2.getLastName()); } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestEmployee { static void printList(List<Employee> empList, String msg) { System.out.println(msg); System.out.println("----------------"); for (Employee emp : empList) { System.out.println(emp); } } public static void main(String args[]) { Employee emp1 = new Employee(1, "Shreyas", "Desai"); Employee emp2 = new Employee(3, "Piyush", "Rai"); Employee emp3 = new Employee(2, "Shanmugam", "Chinnappaiyan"); Employee emp4 = new Employee(5, "Ankit", "Suyal"); Employee emp5 = new Employee(4, "Hari Krishna", "Gurrm"); List<Employee> empList = new ArrayList<Employee>(); empList.add(emp1); empList.add(emp2); empList.add(emp3); empList.add(emp4); empList.add(emp5); printList(empList, "Before sorting"); Collections.sort(empList, new EmployeeIdComparator()); printList(empList, "After sorting by Employee Id"); Collections.sort(empList, new EmployeeFirstNameComparator()); printList(empList, "After sorting by Employee first name"); Collections.sort(empList, new EmployeeLastNameComparator()); printList(empList, "After sorting by Employee last name"); } }
Output
Before sorting ---------------- Employee{id=1firstName=ShreyaslastName=Desai} Employee{id=3firstName=PiyushlastName=Rai} Employee{id=2firstName=ShanmugamlastName=Chinnappaiyan} Employee{id=5firstName=AnkitlastName=Suyal} Employee{id=4firstName=Hari KrishnalastName=Gurrm} After sorting by Employee Id ---------------- Employee{id=1firstName=ShreyaslastName=Desai} Employee{id=2firstName=ShanmugamlastName=Chinnappaiyan} Employee{id=3firstName=PiyushlastName=Rai} Employee{id=4firstName=Hari KrishnalastName=Gurrm} Employee{id=5firstName=AnkitlastName=Suyal} After sorting by Employee first name ---------------- Employee{id=5firstName=AnkitlastName=Suyal} Employee{id=4firstName=Hari KrishnalastName=Gurrm} Employee{id=3firstName=PiyushlastName=Rai} Employee{id=2firstName=ShanmugamlastName=Chinnappaiyan} Employee{id=1firstName=ShreyaslastName=Desai} After sorting by Employee last name ---------------- Employee{id=2firstName=ShanmugamlastName=Chinnappaiyan} Employee{id=1firstName=ShreyaslastName=Desai} Employee{id=4firstName=Hari KrishnalastName=Gurrm} Employee{id=3firstName=PiyushlastName=Rai} Employee{id=5firstName=AnkitlastName=Suyal}
No comments:
Post a Comment