public int compareTo(Object o) { if (!(o instanceof MyString)) { throw new ClassCastException("A MyString object expected."); } MyString other = (MyString) o; if (string.length() == other.length()) { return string.compareTo(other.toString()); } else { return string.length() > other.length() ? 1 : -1; } }