fixed and tested the new sorting logic in form.php