“Comparison method violates its general contract!”

Can someone explain me in simple terms, why does this code throw an exception, “Comparison method violates its general contract!”, and how do I fix it?

private int compareParents(Foo s1, Foo s2) {
    if (s1.getParent() == s2) return -1;
    if (s2.getParent() == s1) return 1;
    return 0;
}

13 Answers
13

Leave a Comment