## Manual binary search by passing just an object

Question

Currently stuck on a problem with binary search that is asking me to pass one parameter with is an object. But is it possible to do this?? Normally I would use two parameters for a problem like this. Normally with binary search I use-->

```
int binarySearch(int[] list, int searchItem)
{
int mid=0;
int start=0;
int end=list.length-1;
boolean found=false;
//Loop until found or end of list.
while (start <= end && !found)
{
mid = (start + end) / 2;
if (list[mid] == searchItem)
found = true;
else
if (list[mid] > searchItem)
end = mid - 1;
else
start = mid + 1;
}
if(found)
return mid;
else
return(-1);
}
```

But is it possible to just pass in one parameter like this?? I need to search an array list.

```
public int binarySearch(Moon searchItem){
int mid = 0;
int start = 0;
int end = moons.size() -1;
boolean found = false;
while(start <= end && !found){
mid = (start + end) / 2;
if(moons.get(mid).equals(searchItem)){
found = true;
}
else{
if(???)) {
}
else
etc etc
}
}
return 0;
}
```

Show source

## Answers to Manual binary search by passing just an object ( 1 )

First you should go through this question: How to compare objects by multiple fields

Then, implement

`Comparable`

to`Moon`

class. If you can't change Moon class, then you've to create`Comparator`

.You'll need to override

`compareTo`

method in Moon class which can be used in place of`???`

in your question.