Hie Paul,
I understand that option two on this question is correct since the compare can use the compareTo method to return a result, however i cannot explain what is wrong with option three:
Collections.sort(al, new Comparable<Person>(){ public int compare(Person o1, Person o2) { return o1.dob.compare(o2.dob); } });
May u please explain to me why option three is not correct
About Question enthuware.ocpjp.v7.2.1725 :
Moderator: admin
-
- Posts: 6
- Joined: Thu Dec 31, 2015 1:40 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1725 :
Well, as the explanation notes, you need to pass a Comparator as the second argument to the Collections.sort method. While option 3 is passing a Comparable. So it won't compile.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 6
- Joined: Thu Dec 31, 2015 1:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1725 :
Sorry i meant option four wich is passing a Comparator:
Collections.sort(al, new Comparator<Person>(){
public int compare(Person o1, Person o2) {
return o1.dob.compare(o2.dob); } });
I dont seem to get what is wrong with it, may you please explain
Collections.sort(al, new Comparator<Person>(){
public int compare(Person o1, Person o2) {
return o1.dob.compare(o2.dob); } });
I dont seem to get what is wrong with it, may you please explain
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1725 :
dob is a String and String doesn't have compare method so the line return o1.dob.compare(o2.dob); will not compile.
You may want to try compiling the code.
HTH,
Paul.
You may want to try compiling the code.
HTH,
Paul.
-
- Posts: 6
- Joined: Thu Dec 31, 2015 1:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1725 :
Thank you Paul, I got it now!
Who is online
Users browsing this forum: No registered users and 8 guests