sortByAmountHighToLow(trades) {
return trades.sort((a, b) => {
return b.total - a.total;
});
},
I have this right now (in Vue) which sorts an array of numbers from high to low, as it should.
What I instead need is a sorting function which does not look at negative numbers differently from positive numbers.
This is the output I would like (high to low): [-531, 245, -195, 54, -12]
I'm getting: [245, 54, -12, -195, -513]
I hope this clearly explains what I need.
CodePudding user response:
I wouldn't say that the original functions treats negatives differently
But this it what you want:
const array = [245, 54, -12, -195, -513]
array.sort((a, b) => Math.abs(b) - Math.abs(a))
console.log(array)
CodePudding user response:
Try this.
trades.sort((a, b) => {
return Math.abs(b) - Math.abs(a);
});