Bug fix in sort