fix sorting functionality