• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays : Help Needed

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic