Fix sorting function