A Comparator compares elements based on its own logic. For example, one Comparator might put 1 before 2. But a different Comparator might put 2 before 1, depending on how it's coded.
So if a collection/array is sorted using a Comparator, then an instance of the same Comparator needs to be used when conducting a binary search. Otherwise, the logic of the search might not be consistent with the logic that was used to sort the collection/array.
Note: I don't think it needs to be the same Comparator instance. It just needs to be an instance of the same Comparator class.
"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
Not even that. The only requirement is that, for sorting Comparator sort and searching Comparator search and any two objects x and y:
- if sort.compare(x, y) < 0 then search.compare(x, y) < 0
- if sort.compare(x, y) = 0 then search.compare(x, y) = 0
- if sort.compare(x, y) > 0 then search.compare(x, y) > 0
In other words, the two Comparators impose the same ordering. Of course using instances of the same class will usually do that, unless instances use instance fields in their sorting decision. If you use the same instance then the above three rules are true by default. Unless again it uses instance fields and those fields are changed.