Array.prototype.sort
sorts the elements of the array in-place and return the sorted array.
The requirements from the compareFunction(a, b)
are:
- get two elements (to compare)
- return
<0
in order to placea
beforeb
- return
0
in order to keep the original position ofa
andb
(relative to each other) - return
>0
in order to placeb
beforea
. - the returned value must always be the same for each pair of elements.
Since every browser-provider might implement the sorting algorithm differently, my question is: is possible to provide a compareFunction
that will cause the sort
function to get into an infinite loop while trying to sort the elements?
If so - in case it is possible - would it be considered a bug in the implementation, or in case the compareFunction
did not follow the above instructions - it's okay to get unexpected results?
To be clear - I'm not asking if it's possible to add
while (true);
inside thecompareFunction
.