Consider following code snippet:

var n = [4, 6, 19, 34, 32];
n.sort();
What will be the value of n variable?
Explanation
JavaScript sort() method by default sorts the array elements as strings in alphabetical and ascending order. [19, 32, 34, 4, 6]; The "4" is bigger than "19" because "4" is bigger than "1". You can fix this by providing the 'compareFix' parameters. For example:

n.sort( function(a,b) {return a-b} )
the result of following will be: [4, 6, 19, 32, 34]

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback