modified sorting function