tile sort logic