Without Collections.sort() :
First implement Comparable<Employee>
in Empoloyee's class and override compareTo
@Override
public int compareTo(Employee o) {
return this.id.compareTo(o.id);
}
Pass your unsorted list to TreeSet
and get set
(sorted on id) then create new List
with this set
List<Employee> list=new ArrayList<Employee>();
list.add(new Employee(1, "A", Double.parseDouble("50")));
list.add(new Employee(22, "B", Double.parseDouble("11")));
list.add(new Employee(3, "C", Double.parseDouble("222")));
list.add(new Employee(34, "D", Double.parseDouble("4")));
SortedSet<Employee> set=new TreeSet<Employee>( list);
List<Employee> l=new ArrayList<Employee>();
l.addAll(set);
System.out.println(l);
OutPut: Sorted on id withoutCollections.sort()
[Employee [id=1, name=A, price=50.0], Employee [id=3, name=C, price=222.0], Employee [id=22, name=B, price=11.0], Employee [id=34, name=D, price=4.0]]
Edit:
Employee class:
class Employee implements Comparable<Employee>{
Integer id;
String name;
Double price;
-------
}