Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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
Javascript IE中的排序数组_Javascript_Jquery_Arrays_Sorting - Fatal编程技术网

Javascript IE中的排序数组

Javascript IE中的排序数组,javascript,jquery,arrays,sorting,Javascript,Jquery,Arrays,Sorting,我的javascript中有一个名为myArr的数组,如下所示 ["ob1","ob10","ob4","ob5","ob12"] 当我对这个数组进行排序时,它不会按数字的顺序排序,因为它是一个字符串数字。因此,我使用下面的方法对数组进行排序,现在它可以工作了 var collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); myArr .sort(collator.compare); 然

我的javascript中有一个名为myArr的数组,如下所示

["ob1","ob10","ob4","ob5","ob12"]
当我对这个数组进行排序时,它不会按数字的顺序排序,因为它是一个字符串数字。因此,我使用下面的方法对数组进行排序,现在它可以工作了

var collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
myArr .sort(collator.compare);

然而,IE9不支持这种方法。有什么方法可以用来在IE中对这种数组进行排序吗?

如果数组元素都遵循一些非数字后跟一些十进制数字的模式,那么可以将字符串拆分为非数字部分和数字部分,然后首先比较非数字部分,然后比较数字部分。(如果非数字部分始终为“ob”,则可能更简单,但这里允许使用其他字符串。)

var数组=[“ob1”、“ob10”、“ob4”、“ob5”、“oc10”、“ob12”、“oc4”];
变量re=/(\D+)(\D+)/;
var sorted=array.sort(函数(a,b){
var aa=a.匹配(re);
var bb=b.匹配(re);
返回(
aa[1]bb[1]?+1:
aa[2]-bb[2]
);
});

控制台日志(已排序)
您可以查看一下,但是当您使用IE9时,您可能无法绕过编辑。如果您使用的是lodash,请确保使用3.10.1,因为IE9支持已在V4中删除。为什么您不能编写自己的比较器?我认为它可以这么简单:
myArr.sort(函数(a,b){var an=parseInt(a.replace(/^\D+/g,);var bn=parseInt(b.replace(/^\D+/g,);return bn-an;})
试试这个
arr.sort((a,b)=>a.localeCompare(b,未定义,{numeric:true})