Why doesn’t the sort function of javascript work well?

It’s sorting alphabetically, try passing your own sorting function:

var x = new Array();
x[0] = 2.73;
x[1] = 11.17;
x[2] = 3.12;

numberSort = function (a,b) {
    return a - b;
};

x.sort(numberSort);

for(var i in x) {
    alert(x[i]);
}

Leave a Comment