aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays : Help Needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays : Help Needed" Watch "Arrays : Help Needed" New topic
Author

Arrays : Help Needed

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Friends,
On page 556 :
Arrays.sort(arrayToSort)
Arrays.sort(arrayToSort, Comparator)

Is 2nd form correct ?



This code does not compile.

Does Arrays.sort(Arraytosort,Comparator) is only for Array of Object ?
Bing Qiao
Greenhorn

Joined: Oct 24, 2006
Posts: 21
I think the argument type of the compare method should be the same (or compatible?) as that of the element of the array to be sorted.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
There are no methods in the Arrays class that allow you to directly sort an array of primitives with a Comparator.

However, if you have the primitives stored in an array of their corresponding wrapper classes, then you can use the sort methods in Arrays and specify a Comparator.
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
Hi, For your code should have to be changed in so many places. First of all you have to give the import statement.And then don't forget for using comparator on some object, means, we have to proivide the comparator for that object. so you have to give two comparators for Float and String.So your final code will be like this,as given below


---Naveen
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
Hi, For your code to compile you have to change in so many places. First of all you have to give the import statement.And then don't forget, for using comparator on some object, means, we have to proivide the comparator for that object. so you have to give two comparators for Float and String.So your final code will be like this,as given below

although this code may not give an efficient comparator !, for compilation of code these all things we have to do.
---Naveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays : Help Needed