optimize sortTasks

@StephanHoyer thanks again!