Here is the code and the results: var cols = [100,200,300,400]; alert('1a: '+cols); cols.sort(); alert('1b: '+cols); cols.reverse(); alert('1c: '+cols); // Displays: "1a: 100,200,300,400", "1b: 100,200,300,400", "1c: 400,300,200,100" var cols = [400,300,200,100]; alert('2a: '+cols); cols.sort(); alert('2b: '+cols); cols.reverse(); alert('2c: '+cols); // Displays: "2a: 400,300,200,100", "2b: 100,200,300,400", "2c: 400,300,200,100" var cols = [888,666,999,777]; alert('3a: '+cols); cols.sort(); alert('3b: '+cols); cols.reverse(); alert('3c: '+cols); // Displays: "3a: 888,666,999,777", "3b: 666,777,888,999", "3c: 999,888,777,666" var cols = [997,998,999,1000]; alert('4a: '+cols); cols.sort(); alert('4b: '+cols); cols.reverse(); alert('4c: '+cols); // Displays: "4a: 997,998,999,1000", "4b: 1000,997,998,999", "4c: 999,998,997,1000" var cols = [998,999,1000,1001]; alert('5a: '+cols); cols.sort(); alert('5b: '+cols); cols.reverse(); alert('5c: '+cols); // Displays: "5a: 998,999,1000,1001", "5b: 1000,1001,998,999", "5c: 999,998,1001,1000" var cols = [1000,100,5000,10000,500]; alert('6a: '+cols); cols.sort(); alert('6b: '+cols); cols.reverse(); alert('6c: '+cols); // Displays: "6a: 1000,100,5000,10000,500", "6b: 100,1000,10000,500,5000", "6c: 5000,500,10000,1000,100" var cols = [300,80,7777,25,500]; alert('7a: '+cols); cols.sort(); alert('7b: '+cols); cols.reverse(); alert('7c: '+cols); // Displays: "7a: 300,80,7777,25,500", "7b: 25,300,500,7777,80", "7c: 80,7777,500,300,25" var cols = [12000,14000,11000,10000,13000]; alert('1a: '+cols); cols.sort(); alert('1b: '+cols); cols.reverse(); alert('1c: '+cols); // Displays: "1a: 12000,14000,11000,10000,13000", "1b: 10000,11000,12000,13000,14000", "1c: 14000,13000,12000,11000,10000" var cols = [12000,14000,1100,10000,13000]; alert('1a: '+cols); cols.sort(); alert('1b: '+cols); cols.reverse(); alert('1c: '+cols); // Displays: "1a: 12000,14000,1100,10000,13000", "1b: 10000,1100,12000,13000,14000", "1c: 14000,13000,12000,1100,10000"
Can you attach a test case as an attachment to the bug? It's hard to follow in the comment like it is.
Created attachment 33675 [details] Test case for numeric array.sort() bad results
Array.prototype.sort defaults to string sort, not numeric sort. Your title says "numeric". Why did you expect this sort to be numeric?
(In reply to comment #3) > Array.prototype.sort defaults to string sort, not numeric sort. Your title says > "numeric". Why did you expect this sort to be numeric? I was expecting a numeric sort since I was not giving it string literals. Having looked at the Javascript core reference, I see that I am incorrect and this is not a bug.
> I was expecting a numeric sort since I was not giving it string literals. > Having looked at the Javascript core reference, I see that I am incorrect and > this is not a bug. Yeah, this is a bit of a gotcha in JavaScript. :(