WebKit Bugzilla
New
Browse
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
RESOLVED INVALID
27781
Javascript numeric array.sort() returns a bad result if one or more elements vary greatly
https://bugs.webkit.org/show_bug.cgi?id=27781
Summary
Javascript numeric array.sort() returns a bad result if one or more elements ...
Jim Perry
Reported
2009-07-28 14:32:15 PDT
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"
Attachments
Test case for numeric array.sort() bad results
(3.59 KB, text/html)
2009-07-28 14:51 PDT
,
Jim Perry
no flags
Details
View All
Add attachment
proposed patch, testcase, etc.
Mark Rowe (bdash)
Comment 1
2009-07-28 14:41:34 PDT
Can you attach a test case as an attachment to the bug? It's hard to follow in the comment like it is.
Jim Perry
Comment 2
2009-07-28 14:51:46 PDT
Created
attachment 33675
[details]
Test case for numeric array.sort() bad results
Geoffrey Garen
Comment 3
2009-07-28 16:31:24 PDT
Array.prototype.sort defaults to string sort, not numeric sort. Your title says "numeric". Why did you expect this sort to be numeric?
Jim Perry
Comment 4
2009-07-28 17:32:30 PDT
(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.
Geoffrey Garen
Comment 5
2009-07-29 09:46:58 PDT
> 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. :(
Note
You need to
log in
before you can comment on or make changes to this bug.
Top of Page
Format For Printing
XML
Clone This Bug