Bug 27781

Summary: Javascript numeric array.sort() returns a bad result if one or more elements vary greatly
Product: WebKit Reporter: Jim Perry <james.r.perry>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Normal CC: ggaren
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Mac (Intel)   
OS: OS X 10.5   
Attachments:
Description Flags
Test case for numeric array.sort() bad results none

Description Jim Perry 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"
Comment 1 Mark Rowe (bdash) 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.
Comment 2 Jim Perry 2009-07-28 14:51:46 PDT
Created attachment 33675 [details]
Test case for numeric array.sort() bad results
Comment 3 Geoffrey Garen 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?
Comment 4 Jim Perry 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.
Comment 5 Geoffrey Garen 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. :(