New issue
Advanced search Search tips

Issue 794860 link

Starred by 2 users

Issue metadata

Status: WontFix
Owner: ----
Closed: Dec 2017
Components:
EstimatedDays: ----
NextAction: ----
OS: Windows
Pri: 2
Type: Bug



Sign in to add a comment

javascript:the function sort() will return a wrong result in chrome

Reported by liukui1...@gmail.com, Dec 14 2017

Issue description

UserAgent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36

Steps to reproduce the problem:
`<script type="text/javascript">
   var a = [1,3,5,7,9,3,4,6,8,10,11,13,15,17,19,12,14,16,18,10,20];
   var b = a.sort(function(x,y){return x<y;});
   console.log(b);
</script>`
the function a.sort() will get a wrong result when the length of a is over 10 in Chrome but it's OK in Firefox

What is the expected behavior?

What went wrong?
`<script type="text/javascript">
   var a = [1,3,5,7,9,3,4,6,8,10,11,13,15,17,19,12,14,16,18,10,20];
   var b = a.sort(function(x,y){return x<y;});
   console.log(b);
</script>`
the function a.sort() will get a wrong result when the length of a is over 10 in Chrome but it's OK in Firefox

Did this work before? N/A 

Chrome version: 63.0.3239.84  Channel: stable
OS Version: 6.1 (Windows 7, Windows Server 2008 R2)
Flash Version:
 

Comment 1 by woxxom@gmail.com, Dec 14 2017

Your code is incorrect - it should return -1, 0, 1, but instead it returns only 0 and 1.
Firefox and any ECMAscript-compliant browser produces unsorted output for your code.

Change "return x<y" to "return x-y" and you'll see the correct result.
Status: WontFix (was: Unconfirmed)

Comment 3 by woxxom@gmail.com, Dec 14 2017

Correction: if you want descending order use "return y-x". 

As a side note, it seems Firefox implements an additional non-spec compliant enhancement by doing a so-called stable sort - the elements for which your code returns 0 get resorted one more time via additional reverse check.

Sign in to add a comment