Fix sorting for gen_schedule, add test file