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
<0in order to placeabeforeb - return
0in order to keep the original position ofaandb(relative to each other) - return
>0in order to placebbeforea. - 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.
.sort()while(true);. I'm asking if it's possible to return specific values that will cause thesortfunction to get into endless-loop while trying to sort.