Скорректированный код из JavaSE:
public static void sort(List<YourObject> list) {
Object[] a = list.toArray();
sort(a);
ListIterator<YourObject> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((YourObject)a[j]);
}
}
public static void sort(YourObject[] a) {
YourObject[] aux = (YourObject[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
private static void mergeSort(YourObject[] src,
YourObject[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
dest[j-1].getPrice()-dest[j].getPrice()>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (src[mid-1].getPrice()-src[mid].getPrice() <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && src[p].getPrice()-src[q].getPrice()<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}