Collections class has a method Sort which is used to sort arraylist.
sort(List<T> list)
sort(List<T> list, Comparator<? super T> c)
|
If List contains String,int... we can use first method.
If the List contains Object type we have to use second method to sort List.
The Second method has parameter of type Comparator which compare the object by
compare(Object o1, Object o2) method
Example
|
import java.util.ArrayList; public class JavaApplication1 { Employee emp2=new Employee("Name 2", 18000, "Junior Developer"); Employee emp3=new Employee("Name 3", 48000, "Manager"); Employee emp4=new Employee("Name 4", 30000, "TeamLead"); @Override
public Employee(String name, int salary, String designation) { public String getName() { public void setName(String name) { public int getSalary() { public void setSalary(int salary) { public String getDesignation() { public void setDesignation(String designation) { |
Output
|
Before Sorting....
|