方法一:使用es6相关语法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e26e41fe1925ed4be9f58671b4eb47dd.png)
const MyObj = { bill: 500, sam: 480, roark: 501, tom: 999 };
const SortValue = Object.values(MyObj).sort((a, b) => {
return b - a;
})
const res = {}
const keys = Object.keys(MyObj);
for (i in SortValue) {
keys.forEach((item) => {
if (SortValue[i] === MyObj[item]) {
res[item] = SortValue[i];
}
})
}
console.log(res);
console.log(Object.keys(res));
方法二:使用冒泡排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0d2c6117b179b40333e2938d13e4160e.png)
const MyObj = { bill: 500, sam: 480, roark: 501, tom: 999 };
const ArrKeys = Object.keys(MyObj);
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());