Inefficient sort implementation