aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Comparator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Comparator question" Watch "K&B Comparator question" New topic
Author

K&B Comparator question

Kevin Crays
Ranch Hand

Joined: Apr 26, 2006
Posts: 41
On page 557 in K&B's book it says
If the collection/array you want to search was sorted in natural order, it must be searched in natural order. (This is accomplished by NOT sending a Comparator as an argument to the binarySearch() method.


Why can't you send a Comparator argument? AFAIK, there's no reason why you can't use a comparator to sort/search in naturual order (even if doing that makes little sense).


We're binary code: a one and a zero<br />You wanted violins and you got Nero
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Yep ! but nobody would do it


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Kevin Crays
Ranch Hand

Joined: Apr 26, 2006
Posts: 41
Originally posted by John Meyers:
Yep ! but nobody would do it


There are lots of things on the test that I can't imagine anyone doing (e.g. Integer Integer = new Integer()), but the quoted paragraph, to me, implies that it cannot be accomplished using a comparator, or that it's illegal to use a comparator.

If it's never tested, then no harm, no foul. Otherwise, I think Bert and Kathy should reword it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think the wording is accurate, but could perhaps be expanded on a bit. (I don't have the book, but I'm curious whether there's more context.)

As you've noted, the idea is that the ordering scheme used by a binary search must match whatever ordering was used to sort the collection or array. If natural ordering was used, then there's no need to provide a Comparator to the search method, although you could provide a natural-ordering Comparator.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
I've added a note to consider softening the wording on page 557. I'm not going to count it as an errata, Maybe we'll add the word "typically" - I'm not sure yet.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Comparator question