The only really tricky part here is the commas in 1,000,000
. You'd need to strip them out before attempting to compare the numbers.
price.sort(function(x, y) {
return y.replace(/,/g,'') - x.replace(/,/g,'');
});
This will produce
["1,000,000", "800", "800", "800", "750",
"700", "700", "600", "500", "500", "400",
"350", "250", "200", "200", "25", "1", "0"]
Or possibly convert the elements to numbers first, then sort the results:
price = price.map(function(x) { return parseInt(x.replace(/,/g,''),10) })
.sort(function(x, y) { return y - x; });
This will produce
[1000000, 800, 800, 800, 750, 700, 700, 600,
500, 500, 400, 350, 250, 200, 200, 25, 1, 0]
Note: Array.prototype.map
was introduced in ECMAScript 5, so it is not available in some older browsers. If this is a concern, the alternative is to use a conventional for-loop to transform the array.