X1043: Complete Comparator for full names

Use the Person class below. Note that this class does NOT implement the Comparable interface.

  public class Person {
    // ...
    public Person (String f, String l) { ... }
    public int compareTo(Person o) { ... }
    public String getFirst() { ... }
    public String getLast() { ... }
    @Override
    public String toString() { return getLast()+", "+getFirst(); }
  }

For this problem you must complete the following class.

public class CompareFullNames implements Comparator<Person>
{
  public int compare(Person o1, Person o2) {
    // your code goes here
  }
}

Complete the compare() method in the class above as described below. The method takes as a parameter two objects of type Person named o1 and o2.

  • If o1 and o2 are both null, return 0.
  • If o1 is null, return -1.
  • If o2 is null, return 1.
  • If the last name in o1 is less than the last name in o2, return -1.
  • If the last name in o1 is greater than the last name in o2, return 1.
  • If the last name in o1 is equal to the last name in o2:.
    • if first name in o1 is less than the first name in o2, return -1.
    • if first name in o1 is equal to the first name in o2, return 0.
    • if first name in o1 is greater than the first name in o2, return 1.

Your Answer:

Feedback

Your feedback will appear here when you check your answer.