【发布时间】:2020-03-03 21:58:25
【问题描述】:
我在那儿,
我有一个分数嵌套数组,我正在尝试对其进行排序,但是所有值都是字符串,并且排序如预期的那样混乱。
也全部保存在本地存储中。
我有下面的排序代码,它工作正常,但仅适用于第一个字符,所以对于索引 [3] 排序,例如我得到:
0000, 126, 15, 16, 20, 4, 4, 4, 5, 8 //如下面的数组所示。
但我应该得到:
0000、4、4、4、5、8、15、16、20、126
我读到我可以在排序之前添加零,然后在排序之后删除它们。但我想不出一种在如此复杂的数据数组中添加零的方法......
数组如下所示:
0: (5) ["Jack 9", "words", "6", "0000", "00:08:21"]
1: (5) ["Jack 6", "words", "59", "126", "00:08:22"]
2: (5) ["Jack 1", "words", "30", "15", "00:03:81"]
3: (5) ["Jack 4", "words", "24", "16", "00:02:94"]
4: (5) ["Jack 2", "words", "9", "20", "00:14:90"]
5: (5) ["Jack 11","words", "9", "4", "00:18:04"]
6: (5) ["Jack 10","words", "9", "4", "00:19:06"]
7: (5) ["Jack 5", "words", "6", "4", "00:08:97"]
8: (5) ["Jack 8", "words", "9", "5", "00:16:83"]
9: (5) ["Jack 7", "words", "8", "8", "00:19:98"]
function sortScoresErrors(array){
array.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(3));// (3) is the index - Descending
};
【问题讨论】:
标签: javascript sorting multidimensional-array