Skip to content

Instantly share code, notes, and snippets.

@fsufitch
Created January 6, 2017 20:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsufitch/18bb4692d5f46b649890f8fd58765fbc to your computer and use it in GitHub Desktop.
Save fsufitch/18bb4692d5f46b649890f8fd58765fbc to your computer and use it in GitHub Desktop.
interface Comparator<T> {
(a: T, b: T): number
}
interface Array<T> {
stableSort(cmp?: Comparator<T>): Array<T>;
}
let defaultCmp: Comparator<any> = (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
Array.prototype.stableSort = function<T>(cmp:Comparator<T> = defaultCmp): T[] {
let self: T[] = this; // for typing
let stabilized = self.map((el, index) => <[T, number]>[el, index]);
let stableCmp: Comparator<[T, number]> = (a, b) => {
let order = cmp(a[0], b[0]);
if (order != 0) return order;
return a[1] - b[1];
}
stabilized.sort(stableCmp);
for (let i = 0; i < self.length; i++) {
self[i] = stabilized[i][0];
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment