in reply to sort and undef

Do you mean like this:
($result) = sort grep{defined()} ($val1, $val2) ;
Note. Usually it might be a good idea to tell whats the wanted result.