On the comparison principle in the calculus of variations