fixed a bug in sorting algo