const testt = { bill: 500, sam: 480, roark: 501, tom: 999 };
得到:
{tom: 999, roark: 501, bill: 500, sam: 480}

方法一:使用es6相关语法

// 需求:JS根据对象属性值排序 输出对应的key值数组
// const testt = { bill: 500, sam: 480, roark: 501, tom: 999 };
// ['tom', 'roark', 'bill', 'sam'] 输出这个
const MyObj = { bill: 500, sam: 480, roark: 501, tom: 999 };

// 降序排序value值
const SortValue = Object.values(MyObj).sort((a, b) => {
return b - a;
})
// console.log(SortValue);//[999, 501, 500, 480]

// 创建结果数组
const res = {}
// keys数组
const keys = Object.keys(MyObj);
// 给value值赋值相应keys值
for (i in SortValue) {
// console.log(i);
keys.forEach((item) => {
if (SortValue[i] === MyObj[item]) {
res[item] = SortValue[i];
}
})
}
console.log(res);//{tom: 999, roark: 501, bill: 500, sam: 480}
console.log(Object.keys(res));//['tom', 'roark', 'bill', 'sam']

方法二:使用冒泡排序

//冒泡排序
const MyObj = { bill: 500, sam: 480, roark: 501, tom: 999 };

// 获得keys数组
const ArrKeys = Object.keys(MyObj);
// console.log(ArrKeys); //['bill', 'sam', 'roark', 'tom']

let temp = null;

for (let i = 0; i < ArrKeys.length - 1; i++) {
for (let j = i + 1; j < ArrKeys.length; j++) {
if (MyObj[ArrKeys[i]] > MyObj[ArrKeys[j]]) {
temp = ArrKeys[i];
ArrKeys[i] = ArrKeys[j];
ArrKeys[j] = temp
}
}
}
console.log(ArrKeys.reverse()); // ['tom', 'roark', 'bill', 'sam']

参考:

​​JS根据对象属性值value排序 输出对应的key值数组​​