Consider the last two iterations of your inner loop on the first iteration of the outer loop:
X === 0
- On the penultimate iteration,
Y === Length - 1
; therefore, this[Y + 1]
is equivalent to this[Length]
, which is beyond the end of the array.
- On the final iteration,
Y === Length
; therefore, this[Y]
will not return anything, because the index is beyond the end of the array.
(this[Y + 1]
will not return anything either.)
You need to fix your code to implement the sort correctly.