fix bugs in sort