[['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']]
JavaScript 这样的数组怎么弄成
[['7','6'],['8','7'],['9','5'],['10','8'],['11','4'],['18','7']]
这样的顺序排列
大神
我想大声告诉你2017-06-12 09:29:27
array = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']]
function sortNumber(a,b){return a[0]-b[0]}
let newArray = array.sort(sortNumber)
newArray 应该就是你要的结果了
PHP中文网2017-06-12 09:29:27
你这个二维数组的排列的结果,好像是用的数组中的每个元素(数组)中的第一个下标的元素去比较的,那就转化成一个一维数组排序不就不好了吗?一维数组排好序,再将原二维数组利用index对应上一维数组排序。
逻辑实现思路:
var twoArray = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']];
var oneArray = [];
twoArray.map(function (item ,index) {
oneArray.push({value: item[0], index: index});
});
console.log(oneArray);
function sortNumber (a, b) {
return a.value - b.value;
}
console.log(oneArray.sort(sortNumber));
// 此时的oneArray已排好序
var newTwoArray = [];
oneArray.map(function (item) {
newTwoArray.push(twoArray[item.index]);
});
console.log(newTwoArray);// 即你要的排序