java - How does the sort() method of the Collection class call the Comparable's compareTo()? -
suppose want sort 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 employee
class implements comparable
, therefore must override compareto()
method.
, have write sorting logic in 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 sort()
call compareto()
method internally?
please see open jdk source codes. guess helps.
132 public static <t extends comparable<? super t>> void more ...sort(list<t> list) { 133 object[] = list.toarray(); 134 arrays.sort(a); 135 listiterator<t> = list.listiterator(); 136 (int j=0; j<a.length; j++) { 137 i.next(); 138 i.set((t)a[j]); 139 } 140 }
collections sort calls arrays.sort
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 , answer on 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 (int i=low; i<high; i++) 1156 (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 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 sorted, copy src dest. 1172 // optimization results in faster sorts 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) dest 1179 for(int = destlow, p = low, q = mid; < 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 }
Comments
Post a Comment