New issue
Advanced search Search tips

Issue 886530 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Sep 19
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Linux
Pri: 2
Type: Bug



Sign in to add a comment

Incorrect output of sort() function in console

Reported by kidult.f...@gmail.com, Sep 19

Issue description

UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36

Steps to reproduce the problem:
1. type
[-5, -4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5].sort((x,y) => x<y)
in console

What is the expected behavior?
output [5, 4, 3, 2, 1, 0, 0, -1, -2, -3, -4, -5]

What went wrong?
output [0, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5], incorrect result

Did this work before? No 

Chrome version: 69.0.3497.81  Channel: stable
OS Version: 69
Flash Version: 

something wrong with sorting implementation
this bug doesn't exist in firefox
 
Your comparison function is incorrect strictly speaking: it returns only 0 and 1 missing -1. JavaScript specification doesn't say stable sort is a must so the observed behavior is correct. Chrome 71 will do a stable sort, but relying on non-specified behavior is still wrong. See  bug v8:90 .

https://www.ecma-international.org/ecma-262/8.0/index.html#sec-array.prototype.sort

    The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order).
    If comparefn is not undefined, it should be a function that accepts two arguments x and y 
    and returns a negative value if x < y, zero if x = y, or a positive value if x > y.
Cc: viswa.karala@chromium.org
Labels: Needs-Feedback Triaged-ET
Able to reproduce the issue on chrome reported version# 69.0.3497.81 using Ubuntu 17.10. But seen different behavior on latest chrome# 71.0.3555.0(please find the attached screenshots for the same).

@Reporter: Please find the attached screenshots for your reference and provide your feedback on it and also please check the comment# 1.

Thanks!

886530 - 69.0.3497.81.png
272 KB View Download
886530 - 70.0.3555.0.png
264 KB View Download
Status: WontFix (was: Unconfirmed)
Yes as per #1 sort is stable in Chrome 71 so this is no longer an issue.
Er, I forgot to show the correct spec-compliant comparison function to sort in descending order: (x,y) => y-x
As for why Firefox still sorts with an incorrect comparison function, it's just their implementation quirk, something one never should rely upon.

Sign in to add a comment