排版后我有一个嵌套数组未排序它应该排序,具体取决于子数组的第二项
var unsorted = [["tag_17",0,4],["tag_18",13,18],["tag_435",6,11]];
var sorted = [["tag_18",13,18], ["tag_435",6,11], ["tag_17",0,4]];
答案 0 :(得分:1)
var a = [
["tag_17", 0, 4],
["tag_18", 13, 18],
["tag_435", 6, 11]
];
a.sort(sortFunction);
console.log(a);
function sortFunction(a, b) {
if (a[0] === b[0]) {
return 0;
}
return (a[1] < b[1]) ? 1 : -1;
}
&#13;
您可以阅读有关sort。
的文档答案 1 :(得分:0)
var unsorted = [["tag_17",0,4],["tag_18",13,18],["tag_435",6,11]];
sorted = unsorted.sort((a,b) => {
return b[1] - a[1];
} )
console.log(sorted);
&#13;