Given the code fragments:
and
Which two modifications enable to sort the elements of the emps list? (Choose two.)
A. Replace line n1 with class Person extends Comparator<Person>
B. At line n2 insert public int compareTo (Person p) { return this.name.compareTo (p.name);
}
C. Replace line n1 with class Person implements Comparable<Person>
D. At line n2 insert public int compare (Person p1, Person p2) { return p1.name.compareTo (p2.name);
}
E. At line n2 insert:
public int compareTo (Person p, Person p2) { return p1.name.compareTo (p2.name);
}
F. Replace line n1 with class Person implements Comparator<Person>
Why not D and F
Because Sort method with one argument needs something that is Comparable
B and C
Option A is incorrect, because Comparator is interface, it should be implemented, not extended.
Option C is correct, because only Comparable allowed here. And option B is correct, because Comparable interface decraler method compareTo with 1 argument.
So correct answer is B, C.
B,C