Skip to content

Instantly share code, notes, and snippets.

@yssharma
Created November 3, 2012 13:39
Show Gist options
  • Save yssharma/4007409 to your computer and use it in GitHub Desktop.
Save yssharma/4007409 to your computer and use it in GitHub Desktop.
A sample method sorting a list by comparable and comparator (Confused Coders)
public void someMethod(){
/** Create a Emp List and add dummy values to Sort **/
List<Employee> myEmpList = new ArrayList<Employee>();
Employee emp = new Employee();
emp.salary = 5000;
emp.name = "Arnold";
myEmpList.add(emp);
emp = new Employee();
emp.salary = 15000;
emp.name = "Clooney";
myEmpList.add(emp);
emp = new Employee();
emp.salary = 500;
emp.name = "Brad";
myEmpList.add(emp);
/** Comparable **/
Collections.sort(myEmpList);
/** Comparator **/
Collections.sort(myEmpList, new SalaryComparator());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment