Your compare() method is not transitive. If A == B and B == C, then A must be equal to C.
Now consider this case:
For A, B, and C, suppose the containsKey() method return these results:
childMap.containsKey(A.getID())returnstruechildMap.containsKey(B.getID())returnsfalsechildMap.containsKey(C.getID())returnstrue
Also, consider orders for A.getId() != B.getId().
So,
AandBwould return0, as outerifcondition will befalse=>A == BBandCwould return0, as outerifcondition will befalse=>B == C
But, A and C, could return -1, or 1, based on your test inside the if block. So, A != C. This violates the transitivity principle.
I think, you should add some condition inside your else block, that performs check similar to how you do in if block.