fix sorting bugs