Table.sort only works sometimes (invalid sorting function for sorting)

Fixed. The logic is still not clear to me but I found an answer after so many keywords:

This thread contains a lua-interpreted shuffler algorithm.