Fixing a bug for sorting results