Use insertion sort for references in Array#sort (#90)

This fixes that Weak Heap Sort isn't stable and thus might swap equal values, which sometimes results in not deep equal arrays of strings, for example. Insertion sort is stable, so it is used for references instead.
This commit is contained in:
Max Graey
2018-05-02 20:33:17 +03:00
committed by Daniel Wirtz
parent 8b5d1d7f74
commit 99bde3a5fa
5 changed files with 240 additions and 1901 deletions

View File

@ -314,9 +314,15 @@ export class Array<T> {
}
return this;
}
return changetype<this>(length < 256
? insertionSort<T,T>(this, comparator)
: weakHeapSort<T,T>(this, comparator)
);
if (isReference<T>()) {
// TODO replace this to stable sort when it implemented
return changetype<this>(insertionSort<T>(this, comparator));
} else {
return changetype<this>(length < 256
? insertionSort<T>(this, comparator)
: weakHeapSort<T>(this, comparator)
);
}
}
}