Thursday, November 30, 2006

Things that drive me nuts

What do you think this code will do?

var array = [4,24,8,627,10];
array.sort();
alert(array);


Or, for the laconic among you, alert([4,24,8,627,10].sort());.

Answer:

It will pop up the following window:


Why? Because, by default, sort() sorts things in lexicographic order.

Ugh.

2 comments: