1

can anyone please tell me how to sort the below array of values using javascript.

var cols = new Array();
cols[0] = 13,000,000;
cols[1] = -20.45;
cols[2] = 0.0;
cols[3] = 10,000;
cols[4] = 750.00
cols[5] = 41.00;

I have tried the below two methods for sorting

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

the results are some thing like below.

cols[0] = -20.45;
cols[1] = 0.0;
cols[2] = 10,000;
cols[3] = 13,000,000;
cols[4] = 41.00;
cols[5] = 750.00

`

5
  • 3
    cols[0] = 13,000,000; That's not going to give you the number you want. Are they actually strings? Commented Aug 3, 2013 at 4:41
  • 13,000,000 and 10,000 will be set as 13 and 10. Commented Aug 3, 2013 at 4:41
  • Actually as 0 and 0. Commented Aug 3, 2013 at 4:42
  • Really? jsfiddle.net/RTMpy Commented Aug 3, 2013 at 4:43
  • Ah, yeah, you're right. The assignment is part of the expression before the first ,. I tested with var which changes the meaning of the , a bit. EDIT: ...actually I tested in the console without var. Too tired right now. Commented Aug 3, 2013 at 4:45

3 Answers 3

2

I suggest you strip the commas so they can be converted to numbers using the +. Then you can use those numeric versions for comparison in your sorting functions.

http://jsfiddle.net/EuQTX/1/

var cols = new Array();
cols[0] = '13,000,000';
cols[1] = '-20.45';
cols[2] = '0.0';
cols[3] = '10,000';
cols[4] = '750.00';
cols[5] = '41.00';

//Removing the commas. You can add more characters to remove to the set
var pattern = /[,]/g

cols.sort(function (a,b){
  //remove unwanted characters so they can be converted to numbers
  a = +a.replace(pattern,'');
  b = +b.replace(pattern,'');
  //use the numeric versions to sort the string versions
  return a-b;
});

console.log(cols);
//["-20.45", "0.0", "41.00", "750.00", "10,000", "13,000,000"] 

Just a side note, you should declare arrays using the literal notation instead:

var cols = ["13,000,000", "-20.45", "0.0", "10,000", "750.00", "41.00"] 
Sign up to request clarification or add additional context in comments.

2 Comments

I am taking the values in cols array from a html table using innerhtml , so the values in col array are going to change every time so may not be able to put it in fixed string format....Also after remove the commas and sorting it can i be able to display the values again in a amount format that is like 13,000,000 again in an html table....
@user2573586 Conversion to numbers does not affect the values of the cols array. It only happens in the sorting function. You can check the demo.
0

Try with this:

var cols = ['13,000,000', '-20.45', '0.0', '10,000', '750.00', '41.00'];
cols.sort(function(a,b){
  var lhs = parseInt(a.replace(/,/g,""));
  var rhs = parseInt(b.replace(/,/g,""));
  return( lhs - rhs);
})

3 Comments

Points to consider here: The values have decimals, parseInt trim off the decimals during conversion. Next, parseInt should always be provided a radix for safe conversion. Otherwise, JS considers your input as an octal number, AFAIK, instead of a decimal.
@JosephtheDreamer: It only considers it an octal if there's a leading 0 before a valid octal integer. e.g. 015 is octal, 018 isn't. ...and I think that may have changed in new browsers.
@CrazyTrain but a single misplaced 0 will mess up the value, and therefore the order. It's best to be safe.
0

http://jsfiddle.net/M5LFg/4/

function sortBy(array, by) {
  return array
    .map(function (_) { return [ _, by(_) ]; })
    .sort(function (a, b) { return a[1] - b[1]; })
    .map(function (_) { return [ _[0] ]; });
}

sortBy(
  '13,000,000 -20.45 0.0 10,000 750.00 41.00'.split(' '),
  function (_) { return parseInt(_.replace(/,/g, ''), 10); }
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.