Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为IE11中的JavaScript问题创建稳定排序_Javascript_Sorting_Internet Explorer_Prototype_Stable Sort - Fatal编程技术网

为IE11中的JavaScript问题创建稳定排序

为IE11中的JavaScript问题创建稳定排序,javascript,sorting,internet-explorer,prototype,stable-sort,Javascript,Sorting,Internet Explorer,Prototype,Stable Sort,我试图为默认的JavaScript.sort()函数创建一个稳定的sort元素。除IE11及以下版本外,我所有浏览器都可以使用它 代码如下: Array.prototype.stableSort = function(cmp) { cmp = !!cmp ? cmp : (function(a, b) { if (a < b) return -1; if (a > b) return 1; return 0; });

我试图为默认的JavaScript.sort()函数创建一个稳定的sort元素。除IE11及以下版本外,我所有浏览器都可以使用它

代码如下:

   Array.prototype.stableSort = function(cmp) {

    cmp = !!cmp ? cmp : (function(a, b) {
      if (a < b) return -1;
      if (a > b) return 1;
      return 0;
    });
    let stabilizedThis = this.map(function(el, index) { return [el, index]; });

    stabilizedThis.sort(function(a, b) {
      let order = cmp(a[0], b[0]);
      if (order != 0) return order;
      return a[1] - b[1];
    });

    for (let i=0; i<this.length; i++) {
      this[i] = stabilizedThis[i][0];
    }
    return this;
  }
注意:为了解决这个问题,我发现——至少,这段代码在IE11中有效:

Array.prototype.stableSort = function(cmp) {

  cmp = !!cmp ? cmp : (function(a, b) {
    if (a < b) return -1;
    if (a > b) return 1;
   return 0;
  });

  // everything here was removed for testing

  return this;
}
Array.prototype.stableSort=函数(cmp){
cmp=!!cmp?cmp:(函数(a,b){
如果(ab)返回1;
返回0;
});
//这里的一切都被移走进行测试
归还这个;
}
当然,这不会排序(或稳定排序),但不会导致语法错误

不幸的是,开发工具和控制台并没有告诉我故障所在的行号

作为参考,我的代码是基于我在这里发现的。他们使用的东西与ES5(IE11的限制)不兼容,所以我不得不把它改大一点。不确定我是否错过了其他的东西


有什么想法吗?

微软IE11不支持ES6,比如
let
语句

Array.prototype.stableSort = function (cmp) {
    cmp = cmp ||function (a, b) {
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };

    var stabilizedThis = this.map(function (el, index) { return [el, index]; });

    stabilizedThis.sort(function (a, b) {
        return cmp(a[0], b[0]) || a[1] - b[1];
    });

    for (var i = 0; i < this.length; i++) {
        this[i] = stabilizedThis[i][0];
    }
    return this;
}
您可以用
var
语句替换它

Array.prototype.stableSort = function (cmp) {
    cmp = cmp ||function (a, b) {
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };

    var stabilizedThis = this.map(function (el, index) { return [el, index]; });

    stabilizedThis.sort(function (a, b) {
        return cmp(a[0], b[0]) || a[1] - b[1];
    });

    for (var i = 0; i < this.length; i++) {
        this[i] = stabilizedThis[i][0];
    }
    return this;
}
Array.prototype.stableSort=函数(cmp){
cmp=cmp | |函数(a,b){
如果(ab)返回1;
返回0;
};
var stabilizedThis=this.map(函数(el,index){return[el,index];});
稳定此排序(函数(a,b){
返回cmp(a[0],b[0])| a[1]-b[1];
});
for(var i=0;i
您能提供导致故障的真实数据吗?ie11不支持ES6,如
let
@NinaScholz谢谢!我想当我用
var
替换
let
时,它已经修复了。令人恼火的是,我以前做过这件事,但失败了,但那是因为我最终删除了另一个ES6用法。但不管出于什么原因,我添加了
let
返回。你可以加上这个作为答案!