Issue metadata
Sign in to add a comment
|
The sort() method returns different results
Reported by
1piotrma...@gmail.com,
Oct 31
|
||||||||||||||||||||||
Issue description
UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Steps to reproduce the problem:
Execute the following code in dev tools console:
let arr = [{name: 'item A', index: 1},{name:'item B', index: 1}];
console.log(arr.sort((a,b) => a.index < b.index ? 1 : -1).map(i => i.name));
console.log(arr.sort((a,b) => a.index < b.index ? 1 : -1).map(i => i.name));
console.log(arr.sort((a,b) => a.index < b.index ? 1 : -1).map(i => i.name));
console.log(arr.sort((a,b) => a.index < b.index ? 1 : -1).map(i => i.name));
What is the expected behavior?
Above code SHOULD return the following output:
["item A", "item B"]
["item A", "item B"]
["item A", "item B"]
["item A", "item B"]
What went wrong?
Actually above code returns the following output:
["item B", "item A"]
["item A", "item B"]
["item B", "item A"]
["item A", "item B"]
This is wrong, sorting the same array using the same compare function returns different results.
Did this work before? Yes Chrome 69
Chrome version: 70.0.3538.77 Channel: stable
OS Version: 10.0
Flash Version:
,
Oct 31
Ok, so please try the following example:
let arr = [{name: 'item A', index: 1},{name:'item B', index: 1}];
console.log(arr.sort((a,b) => -1).map(i => i.name));
console.log(arr.sort((a,b) => -1).map(i => i.name));
console.log(arr.sort((a,b) => -1).map(i => i.name));
console.log(arr.sort((a,b) => -1).map(i => i.name));
The result is the same. Is it still comparision function's fault?
,
Nov 1
Your new function has the same bug, it behaves exactly the same as the old one for identical arguments. I don't know how else to explain (try asking on StackOverflow) except for showing the correct comparison function for a numeric "index" field: (a,b) => b.index - a.index
,
Nov 1
Able to reproduce the issue on reported chrome version 70.0.3538.77 and on latest chrome# 72.0.3597.0 using Windows-10, Mac 10.12.6 & Ubuntu 17.10, hence providing Bisect Info Bisect Info: ================ Good build: 70.0.3530.0 Bad build: 70.0.3531.0 On performing per-revision bisect facing error, hence providing below change log from chromium bisect You are probably looking for a change made after 585456 (known good), but no later than 585470 (first known bad). Change Log: https://chromium.googlesource.com/chromium/src/+log/ac0405676146ce76211b297659aecd91714564f5..548ac6ffdaac420fbc90a96f09104ef401bc2c38 Suspecting: https://chromium.googlesource.com/v8/v8/+/9e48a24fd9b88712e4ec591c8b1fd40dc6381f18 from above change log Change-Id: I7cd4287e4562d84ab7c79c58ae30780630f976de Reviewed-on: https://chromium-review.googlesource.com/1151199 Note: As the author last visit is more than 30 days, hence assigning it to reviewer(Camillo Bruni) @Camillo Bruni: Please confirm the issue and help in re-assigning if it is not related to your change. Adding ReleaseBlock-Stable for M-71, feel free to remove it if not applicable. Thanks!
,
Nov 2
woxxom@ described the problem correctly. If the compare function does not return 0 for equal elements, the sorting algorithm performs a swap each time it sees these two equal elements. This might seem confusing, but it's the only sane way the sorting algorithm can behave.
,
Nov 2
Ok, so, could you explain me why it was working correctly in previous Chrome versions? And why it performs a swap only when the return value is -1, not 1 also?
,
Nov 2
We've change the sorting algorithm which performs compare and swaps in different order now. Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort the rest depends on the implementation of the sorting algo, we're reling on TimSort https://en.wikipedia.org/wiki/Timsort. |
|||||||||||||||||||||||
►
Sign in to add a comment |
|||||||||||||||||||||||
Comment 1 by woxxom@gmail.com
, Oct 31