Consider the following array:
var fees = [
'$0.9 + $0.1',
'$20 + $2',
'$0.7 + $0.4',
'$5 + $0.5',
'$0 + $0.01',
'$100 + $9',
'$1 + $1',
'$2 + $0.5'
];
If I wanted to sort these string values in numeric ascending order using vanilla JavaScript, how would I go about it?
The expected output after sorting should be:
function mySort(a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
However, this only gives me:
"$0.9 + $0.1"
and"$0.7 + $0.4"
, the sum favors the former, but I want to prioritize the latter. Essentially, I aim to sort based on the first number in ascending order, and if the first numbers match, then consider the second number for sorting.