-2
public int compareTo(Name other) {
    int result = this.familyName.compareTo(other.familyName);

    if (result == 0) {
        result = this.firstName.compareTo(other.firstName);
    }

    return result;
}

I can't comprehend the meat of the code, how it is used to compare names.

banana1337
  • 25
  • 1
  • 4

2 Answers2

0

If the family names are the same, then it compares the first name.

Essentially "grouping by" family name.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

This is the compareTo method of a class implementing the Comparable (see https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) interface. The return value of compareTo is defined to be 0 if the objects are identical, < 0 if the argument is lexicographically greater, and > 0 if the argument is less.

The result of the comparison of the Name object you have here is delegated to the compareTo method of the familyName attribute. That means the familyName attribute of the current Name object is compared to the argument's familyName attribute. The second compareTo check is only done if the familyName attributes of both Name object instances are identical. If that's the case, the firstName is compared instead.

Lupinity Labs
  • 2,099
  • 1
  • 15
  • 23