function stableSort(arr, opt_compareFn) {
  const compArr = new Array(arr.length);
  for (let i = 0; i < arr.length; i++) {
    compArr[i] = {index: i, value: arr[i]};
  }
  const valueCompareFn = opt_compareFn || defaultCompare;
  function stableCompareFn(obj1, obj2) {
    return valueCompareFn(obj1.value, obj2.value) || obj1.index - obj2.index;
  }
  sort(compArr, stableCompareFn);
  for (let i = 0; i < arr.length; i++) {
    arr[i] = compArr[i].value;
  }
}
Sorts the specified array into ascending order in a stable way.  If no
opt_compareFn is specified, elements are compared using
<code>defaultCompare</code>, which compares the elements using
the built in < and > operators.  This will produce the expected behavior
for homogeneous arrays of String(s) and Number(s).

Runtime: Same as `Array.prototype.sort`, plus an additional
O(n) overhead of copying the array twice.

@param {Array<T>} arr The array to be sorted.
@param {?function(T, T): number=} opt_compareFn Optional comparison function
    by which the array is to be ordered. Should take 2 arguments to compare,
    and return a negative number, zero, or a positive number depending on
    whether the first argument is less than, equal to, or greater than the
    second.
@template T