See
https://eu.swi-prolog.org/pldoc/man?section=standardorder
for the "Standard Order of Terms".
Beware the types:
?- compare(D,null,"null"). D = (>). ?- compare(D,"null",null). D = (<). ?- compare(D,null,null). D = (=). ?- compare(D,"null","null"). D = (=).
You may want to use assertion/1 to check your code for surprises here.
This I consider crazy (sorting by the names of variables, because logically compare/3 shouldn't have any access to those variable names; variable names are a feature of the source code, not of the process active at runtime). What should be returned here? Equality of course. These are unbound variables!
?- compare(D,X,Y). D = (<). ?- compare(D,Y,D). D = (>). ?- compare(D,X,X). D = (=).