New issue
Advanced search Search tips

Issue 806231 link

Starred by 2 users

Issue metadata

Status: WontFix
Owner: ----
Closed: Jan 2018
Components:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 2
Type: Bug-Regression



Sign in to add a comment

Array.sort doesn't honor the 0 value.

Reported by michael....@gmail.com, Jan 26 2018

Issue description

UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

Steps to reproduce the problem:
1. In console, paste:
var list = [];
var i = 0;
while (i < 90) {
  list.push({value: i});
  i++;
}
list.sort(function(a,b){return 0;});

What is the expected behavior?
The list order should remain untouched after sorting.

What went wrong?
The 45th and 1st values have swapped.

Did this work before? Yes I'm not sure.

Chrome version: 63.0.3239.132  Channel: n/a
OS Version: OS X 10.12.5
Flash Version:
 

Comment 1 by woxxom@gmail.com, Jan 26 2018

Chrome's V8 never produced stable sort in any version.
JavaScript specification doesn't say stable sort is a must so the observed behavior is correct.

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.

See also https://bugs.chromium.org/p/v8/issues/detail?id=103
That's a fair assessment based on the ecma standard. I'll advise my clients to use Safari while I update sorting methods based on previous Chrome behavior. I understand your goal is probably to maximize sort speed with the addition of quicksort - but, in my opinion, its not worth it to take away a commonly used behavior that has a poorly worded standard.

Comment 3 by woxxom@gmail.com, Jan 26 2018

JavaScript specification never required the sort to be stable so it won't make sense for Chrome to change its correct behavior suddenly after all the years to implement a non-standard quirk provided by other browsers. If you think the ECMA standard is poorly worded, you can open an issue on their bugtracker.
Status: WontFix (was: Unconfirmed)

Sign in to add a comment