这是我的数组:
var myArray = [
'<@424507945156784498> - 152,800$',
'<@381223410610501732> - 100$',
'<@224451506344606852> - 74,424$',
'<@101441124537903160> - 65,100$'
];
这就是我想要的:
var newArray = [
'<@424507945156784498> - 152,800$',
'<@224451506344606852> - 74,424$',
'<@101441124537903160> - 65,100$'
'<@381223410610501732> - 100$',
];
我尝试使用此方法 - Javascript : natural sort of alphanumerical strings 但它按 id 排序 (<@424507945156784498>) 我如何按货币值(value)排序?
最佳答案
拆分和替换 - 排序将对现有数组进行排序 - 如果您不需要改变它,则需要将其复制到另一个数组:
var myArray = [
'<@424507945156784498> - 152,800$',
'<@381223410610501732> - 100$',
'<@224451506344606852> - 74,424$',
'<@101441124537903160> - 65,100$'
];
function toNum(str) {
return +str.split(" - ")[1] // get the amount
.replace(/[^\d]/g,""); // remove all non-numeric
}
myArray.sort(function(a,b) {
return toNum(b)-toNum(a); // numeric sort in situ
});
console.log(myArray)
关于javascript - 用数字对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102231/