It sounds to me like you want the servo to move the ultrasonci rangefinder from side to side and note the direction to the (nearest?) object.
To do that you need the File->Examples->Servo->Sweep for a start (that sweeps the servo side to side) and you need, at each position, to measure the distance and see if the measures distance is less than the previous nearest distance. If it is, store the servo angle and store the current distance as the previous nearest distance.
Note: Because you will be measuring the distance in both the left-to-right and right-to-left sweeps it would be good to write a function to do the measurement instead of putting all the code in both places.