Issue #1994880 by 1an_m: Improved comparebyOptionals function.

Issue #1994880 by 1an_m: Improved comparebyOptionals function.