array.sort() not giving correct result
Reported by
skmishra...@gmail.com,
Aug 23 2017
|
|||||
Issue descriptionChrome Version (from the about:version page): Version 61.0.3163.49 (Official Build) beta (64-bit) Is this the most recent version: yes OS + version: ubuntu 17.04 CPU architecture (32-bit / 64-bit): 64-bit Window manager: URLs (if relevant): Behavior in Linux Firefox: Behavior in Windows Chrome (if you have access to it): What steps will reproduce the problem? (1)open the chrome dev tool (2)In console, declare an array and use array.sort() method (3)It is not giving correct result What is the expected result? What happens instead? Please provide any additional information below. Attach a screenshot and backtrace if possible. For graphics-related bugs, please copy/paste the contents of the about:gpu page at the end of this report.
,
Aug 23 2017
It's not a bug actually, but a specification-compliant behavior: Array.sort uses lexical order, not numeric. See also https://bugs.chromium.org/p/v8/issues/detail?id=5963 Other browsers also behave this way.
,
Aug 23 2017
More info: * ECMA spec says each element is converted to a string while sorting if you don't provide a comparator: >Let elem be obj.[[GetOwnProperty]](! ToString(i)). https://tc39.github.io/ecma262/#sec-array.prototype.sort * MDN >The default sort order is according to string Unicode code points. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort * solutions: https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly
,
Aug 23 2017
Er, ECMA spec quote was incorrect, here's the correct one in case comparator is not defined: >5. Let xString be ? ToString(x). >6. Let yString be ? ToString(y).
,
Aug 24 2017
@skmishra: Can you confirm is your issue as same as issue 5963 ? Thanks!!
,
Aug 24 2017
No..this is not the same issue
,
Aug 24 2017
Thank you for providing more feedback. Adding requester "sandeepkumars@chromium.org" to the cc list and removing "Needs-Feedback" label. For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
,
Aug 24 2017
Not issue 5963 in chromium, but in v8: https://bugs.chromium.org/p/v8/issues/detail?id=5963 The underlying assumption of bug reports is the same - in both cases the OP expected array.sort to do a numeric comparison whereas the specification says it's lexical i.e. uses a string representation of each value. The answer is the same: working as intended according to the specification. Use a custom comparator function arr.sort((a,b) => a-b)
,
Aug 29 2017
Thanks for filing the issue, able to reproduce the issue as specified on Chrome stable version 60.0.3112.113. issue is a non regression as observed from M24 (24.0.1300.0) builds. Issue is observed on Windows 10, Mac 10.12.4, Ubuntu 14.04 OS as well. Untriaged it so that issue gets addressed.
,
Aug 30 2017
JavaScript is broken, not us :(
,
Oct 17
Issue 896286 has been merged into this issue. |
|||||
►
Sign in to add a comment |
|||||
Comment 1 Deleted