[Solved] Is there the matter with the compareTo return value? [closed]


… why do almost the all method implementations return only -1, 0, 1?

I can’t speak for other programmers, but I typically do this because it is simpler and more convenient to do that in most case. And most important, it is correct.

I imagine that you are thinking along the lines of doing this:

  public int compareTo (MyClass other) {
      return this.intField - other.intField;
  }

Beware. This code is subtly wrong. See this Q&A: Java Integer compareTo() – why use comparison vs. subtraction?

1

solved Is there the matter with the compareTo return value? [closed]