Project: v8 Issues People Development process History Sign in
New issue
Advanced search Search tips
Issue 2264 Array.prototype.sort does not call ToString() for identical elements
Starred by 1 user Reported by, Jul 29 2012 Back to list
Status: Invalid
Closed: Apr 2015
HW: ----
OS: ----
Priority: ----
Type: ----

Sign in to add a comment
The SortCompare abstract operation (cf. Array.prototype.sort []) calls ToString() for each element which is going to be compared. This ToString() call is currently omitted for identical elements, this is not spec compliant. 

test case:
var c = 0;
var o = {toString: function(){ c++; return ""; }}
[o, o].sort();

Expected output:

Actual output:
Note the missing ";" after the object literal
Status: Assigned
The standard is intentionally vague about how many times SortCompare is called.  Is this result not consistent with SortCompare not being called at all?

"Perform an implementation-dependent sequence of calls to the [[Get]] , [[Put]], and [[Delete]] internal methods of obj and to SortCompare"

In this case the implementation-dependent sequencce does not include any calls to SortCompare.
SortCompare is being called in the current code, but the current implementation of SortCompare [1] does not follow the specification (see line 752 [2]). 

Status: Invalid
When the identity short cut is used, the user of the sort function cannot detect that SortCompare is called at all, therefore V8s behaviour is indistinguishable from a sort routine that is merely clever at minimising how often it calls the comparison function.  This is allowed.
Sign in to add a comment