fix sorting algorithm