JS二维数组排序的问题
如下代码,我希望根据区域的总数进行排序(即对各区域数值进行求和,在根据求和结果进行排序)。不知道应该这么写,请各位大佬指教,感谢。
const arrData = [
[东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 29, 32, 3, 94, 90, 97, 97],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
const arr = arrData.sort(function (a, b) {
console.log(a.slice(1))
return a.slice(1) - b.slice(1)
})
console.log('arr', arr) const arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 29, 32, 3, 94, 90, 97, 97],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
function getTotal(_array) {
var _count = 0;
var _t = _array.length;
while (_t--) {
var _n = Number(_array);
if (!isNaN(_n)) {
_count += _n;
}
}
return _count;
}
const arr = arrData.sort(function (a, b) {
return getTotal(a) - getTotal(b);
})
console.log('arr', arr) const arr = arrData.sort(function (a, b) {
return eval(a.slice(1).join("+")) - eval(b.slice(1).join("+"))
})
这样也行,但不太灵活 let arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 29, 32, 3, 94, 90, 97, 97],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
let res = arrData.sort((a,b)=>a.slice(1).reduce((p,v)=>p+v)-b.slice(1).reduce((p,v)=>p+v));
console.log(res); 本帖最后由 lcbreak 于 2023-2-13 20:43 编辑
const arrData = [
['东区', 50, 67, 'aa', 17, 67, 50, 0], //==>251
['西区', 32, 45, 13, 65, 48, 97, 3],//==>303
['南区', 35, 3, 0, 10, 16, 97, 97], //==>258
['北区', 29, 32, 3, 94, 90, 97, 97],//==>442
['中区', 33, 33, 33, 50, 33, 33, 33]//==>248
];
function getTotal(arr) {
let count = 0;
let index = arr.length-1;
while (index) {
if (!isNaN(arr)) {
count += arr;
}
index--;
}
return count
}
const arr = arrData.sort((a, b) => getTotal(a) - getTotal(b))
console.log(arr);
这样应该符合要求。 const arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 1, 2, 3, 1, 1, 97, 1],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
const arr = arrData.sort(function (a, b) {
let aTotal = 0;
let bTotal = 0;
a.slice(1).forEach((num) => {
aTotal += num;
})
b.slice(1).forEach((num) => {
bTotal += num;
})
return aTotal - bTotal
})
arr.forEach(arr => {
console.log(arr)
})
在你代码基础上稍微调整了一下 本帖最后由 涛之雨 于 2023-2-14 00:36 编辑
```js
const arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 29, 32, 3, 94, 90, 97, 97],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
const val=arrData.map(a=>,a.slice(1).reduce((a,b)=>a+b,0)]).sort((a,b)=>a-b)
console.log(val)
```
emmm我是不是理解错题目了。。。问题不大,楼上有正确的
本帖最后由 侃遍天下无二人 于 2023-2-14 01:37 编辑
这样?是不是很优雅
const arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 1, 2, 3, 1, 1, 97, 1],
['中区', 33, 33, 33, 50, 33, 33, 33]
]
arrData.map(x => {
return ,(x+x+x+x+x+x+x) ]
}).sort( (x,y)=> {
return x - y
})
稍微优化了下,这样就更省内存了
arrData.sort( (x,y) => {
let result = 0
for(let i=1;i<=7;i++){
result += x-y
}
return result
}) const arrData = [
['东区', 50, 67, 0, 17, 67, 50, 0],
['西区', 32, 45, 13, 65, 48, 97, 3],
['南区', 35, 3, 0, 10, 16, 97, 97],
['北区', 29, 32, 3, 94, 90, 97, 97],
['中区', 33, 33, 33, 50, 33, 33, 33]
];
const arr = arrData.sort(function (a, b) {
const sumA = a.slice(1).reduce((acc, cur) => acc + cur);
const sumB = b.slice(1).reduce((acc, cur) => acc + cur);
return sumB - sumA;
});
console.log('arr', arr);
页:
[1]