fix a bug in sorting