Fix sorting for scope change