-2

Here is the array:

var price = new Array("600", "250", "750", "400", "200", "500", "350", "800", "200", "700", "800", "700", "1", "800", "500", "25", "0", "1,000,000");

I want be able to sort and print them out using a simple sort.

People were asking, so I want to convert them into numbers and sort from highest to lowest.

  • 1
    sort as numbers or sort as strings? – Dr.Molle Dec 13 '13 at 16:55
  • Answered nicely here - http://stackoverflow.com/a/1063027/448865 – cpreid Dec 13 '13 at 16:59
  • This question does not deserve all these downvotes. JavaScript's native sort function doesn't sort arrays of "string integers" as one might expect. This is not a trivial question. – cpreid Dec 13 '13 at 17:00
  • 2
    @cpreid - While I have a tendency to get annoyed by how quickly questions get downvoted around here, this is a REALLY easy one to look up (and, as you pointed out, has already been answered on SO), and it shows no attempts at trying to figure out the problem on his own. I'm not going to downvote it, but I have a hard time scolding others for doing it, in this case. – talemyn Dec 13 '13 at 17:08
  • @cpreid FWIW, the problem is not that it's a bad question or doesn't belong on this site, but that OP hasn't demonstrated any attempt to solve it themselves. If there was even so much as a "*I tried `sort` but it didn't work*" it probably wouldn't have been downvoted so heavily. – p.s.w.g Dec 13 '13 at 17:14
  • I agree with @talemyn. – Ariel Dec 13 '13 at 17:15
  • Sorry about that. I tried converting all the string values to integers individually by creating a new var for each one, but couldn't really figure out where to go from there. I love all of these great responses, but unfortunately don't know quite how to call them. – Ian Freeman Dec 13 '13 at 17:18
  • @user3100201 - don't worry too much about it. :) The rules around here take a little getting used to. Keep trying, keep asking questions, and people will keep helping. – talemyn Dec 13 '13 at 17:24

5 Answers5

2

You need to write a comparitor function to compare numbers

price.sort(function(a,b) {
   return a-b;
});

This will sort your array in ascending fashion (b-a for descending). Note this will also modify the order of the original array.

Change to a.replace(/,/g,"") - b.replace(/,/g,"") to ignore commas in number comparisons

Update If you decide that the list can be a mix of numbers or strings you may want to change the above to String(a).replace(/,/g,"") - String(b).replace(/,/g,"") to avoid the syntax error. How you handle non number strings is up to you right now they are being made NaN which is not a useful value for sort comparisons.

megawac
  • 10,953
  • 5
  • 40
  • 61
  • This is clever but it's a bit misleading and I'm not 100% sure it'll work on all browsers. – Ariel Dec 13 '13 at 16:59
  • Works in every browser I can think of since ie6 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#AutoCompatibilityTable – megawac Dec 13 '13 at 17:03
  • This is not misleading at all. The `-` operator will ALWAYS convert both operands to numbers. – Tibos Dec 13 '13 at 17:04
  • Good to know megawac. I still think it's bad practice to subtract strings though. Without seeing the array it could mislead someone to think the values are numbers and not strings when used later on in the code. Unless you're coding alone and will remember everything forever. – Ariel Dec 13 '13 at 17:04
2

If the numbers are always ints you can do the following:

price.sort(function(a, b) {
    return parseInt(a.replace(/,/g,'') - parseInt(b.replace(/,/g,'')));
});

That will sort the original array by the value of the number in ascending order (smallest first). You can reverse it by swapping a and b

Ariel
  • 4,502
  • 2
  • 21
  • 23
1

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.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

You can use the usual sort function: http://www.w3schools.com/jsref/jsref_sort.asp

You can also parse the strings to numbers with "parseInt" or "parseFloat".

Ivozor
  • 976
  • 8
  • 23
-2

Send your customized comparator while sorting the array.

price.sort(function(a, b) {
// write your custom comparison here
})
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 1
    I `-1`'d this, as [Ariel's](http://stackoverflow.com/a/20571930/1454048) and [megawac's](http://stackoverflow.com/a/20571860/1454048) answers both included the actual comparator statement. – admdrew Dec 13 '13 at 16:59