希望将如下对象依据value进行排序,并且保持key与value对应
let obj = {ATTACK: 698462, AV: 4174, FILTER: 7589767, IPS: 1106719, SCAN: 1667425, …};
函数
function objSortByVal(obj) {
var keyArr = [],valArr = [];
for (var key in obj) {
keyArr.push(key);
valArr.push(obj[key]);
}
for (var i = 0, len = valArr.length; i < len; i++) {
for (var j = 0; j < len - i; j++) {
var keyTemp, valTemp;
if (valArr[j] < valArr[j + 1]) {
valTemp = valArr[j];
valArr[j] = valArr[j + 1];
valArr[j+1] = valTemp;
keyTemp = keyArr[j];
keyArr[j] = keyArr[j + 1];
keyArr[j+1] = keyTemp;
}
}
}
var newobj={};
for(var i = 0;i<valArr.length;i++){
newobj[keyArr[i]]=valArr[i];
}
return newobj;
}
调用
objSortByVal(obj)
引用: https://blog.csdn.net/qq_38402659/article/details/107978480