Javascript Array stableSort(f)

Description

Javascript Array stableSort(f)


Array.prototype.stableSort = function(f) {
  return this.map((ele, i) => ({ele: ele, i: i}))
  .sort((a, b) => {//from   w  w  w .j a  v a2  s . c o  m
    var val = f(a.ele, b.ele);
    if (val === 0) {
      return a.i - b.i;
    }
    return val;
  }).map(ele => {
    return ele.ele;
  });
};



PreviousNext

Related