sorting function fixed