Page 1 of 1

About Question enthuware.ocpjp.v7.2.1725 :

Posted: Thu Dec 31, 2015 2:15 am
by stobayiwa
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

Re: About Question enthuware.ocpjp.v7.2.1725 :

Posted: Thu Dec 31, 2015 8:42 am
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1725 :

Posted: Thu Dec 31, 2015 9:22 am
by stobayiwa
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

Re: About Question enthuware.ocpjp.v7.2.1725 :

Posted: Thu Dec 31, 2015 9:31 am
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1725 :

Posted: Thu Dec 31, 2015 9:33 am
by stobayiwa
Thank you Paul, I got it now!