logic fix in sorting