Suppose I want to sort a list of Employee
objects:
Employee emp1 = new Employee("Abhijit", 10);
Employee emp2 = new Employee("Aniket", 5);
Employee emp3 = new Employee("Chirag", 15);
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
Collections.sort(employees);
System.out.println("sorted List is: "+employees);
And my Employee
class implements Comparable
, therefore it must override the compareTo()
method.
And I have to write my sorting logic in the compareTo()
method.
class Employee implements Comparable<Employee>
{
String name;
int empId;
public Employee(String name, int empId)
{
this.name= name;
this.empId = empId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getEmpId()
{
return empId;
}
public void setEmpId(int empId)
{
this.empId = empId;
}
@Override
public int compareTo(Employee e)
{
// TODO Auto-generated method stub
return this.empId > e.empId ? 1 : (this.empId < e.empId ? -1 : 0);
//return 0;
}
@Override
public String toString()
{
return String.valueOf(empId);
}
}
How does sort()
call the compareTo()
method internally?
Please see open jdk source codes. I guess it helps.
java.util.Collections:
132 public static <T extends Comparable<? super T>> void More ...sort(List<T> list) {
133 Object[] a = list.toArray();
134 Arrays.sort(a);
135 ListIterator<T> i = list.listIterator();
136 for (int j=0; j<a.length; j++) {
137 i.next();
138 i.set((T)a[j]);
139 }
140 }
Collections sort calls Arrays.sort
java.util.Arrays:
1218 public static <T> void More ...sort(T[] a, Comparator<? super T> c) {
1219 T[] aux = (T[])a.clone();
1220 if (c==null)
1221 mergeSort(aux, a, 0, a.length, 0);
1222 else
1223 mergeSort(aux, a, 0, a.length, 0, c);
1224 }
Arrays.sort calls Arrays.mergeSort and your answer is on the line 1157.
1145
1146 private static void More ...mergeSort(Object[] src,
1147 Object[] dest,
1148 int low,
1149 int high,
1150 int off) {
1151 int length = high - low;
1152
1153 // Insertion sort on smallest arrays
1154 if (length < INSERTIONSORT_THRESHOLD) {
1155 for (int i=low; i<high; i++)
1156 for (int j=i; j>low &&
1157 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
1158 swap(dest, j, j-1);
1159 return;
1160 }
1161
1162 // Recursively sort halves of dest into src
1163 int destLow = low;
1164 int destHigh = high;
1165 low += off;
1166 high += off;
1167 int mid = (low + high) >>> 1;
1168 mergeSort(dest, src, low, mid, -off);
1169 mergeSort(dest, src, mid, high, -off);
1170
1171 // If list is already sorted, just copy from src to dest. This is an
1172 // optimization that results in faster sorts for nearly ordered lists.
1173 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
1174 System.arraycopy(src, low, dest, destLow, length);
1175 return;
1176 }
1177
1178 // Merge sorted halves (now in src) into dest
1179 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1180 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
1181 dest[i] = src[p++];
1182 else
1183 dest[i] = src[q++];
1184 }
1185 }