New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 758083 link

Starred by 2 users

Issue metadata

Status: WontFix
Owner: ----
Closed: Aug 2017
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Linux , Windows , Mac
Pri: 3
Type: Bug



Sign in to add a comment

array.sort() not giving correct result

Reported by skmishra...@gmail.com, Aug 23 2017

Issue description

Chrome 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.


 
image.jpg
40.6 KB View Download

Comment 1 Deleted

Comment 2 by woxxom@gmail.com, 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.

Comment 3 by woxxom@gmail.com, 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

Comment 4 by woxxom@gmail.com, 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).

Labels: Needs-Triage-M61 Needs-Feedback
@skmishra: Can you confirm is your issue as same as  issue 5963 ?

Thanks!!
No..this is not the same issue
Project Member

Comment 7 by sheriffbot@chromium.org, Aug 24 2017

Cc: sandeepkumars@chromium.org
Labels: -Needs-Feedback
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

Comment 8 by woxxom@gmail.com, 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)
Cc: ranjitkan@chromium.org
Components: Platform>DevTools
Labels: M-62 OS-Mac OS-Windows
Status: Untriaged (was: Unconfirmed)
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.
Status: WontFix (was: Untriaged)
JavaScript is broken, not us :(
 Issue 896286  has been merged into this issue.

Sign in to add a comment