I was given some code that contains an Array of Person objects I am to write methods to do the binary search and to override the compareto method in the Person class to compare based on last name and then first name.
public static int binarySearch( Person[] persons, Person key )
{
int low = 0;
int high = persons.length - 1;
return binarySearch(persons, key, low, high);
}
private static int binarySearch( Person[]persons, Person key, int low, int high )
{
if(low > high) //The list has been exhausted without a match.
return -low - 1;
int mid = (low + high) / 2;
if (persons[mid] == key)
return mid;
if(persons[mid] < key) //!!**'The < operator is undefined for the type'
return binarySearch(persons, key, low, mid-1);
else
return binarySearch(persons, key, 0, persons.length -1);
}
I think I have most of the binary search code write. However, the problem I'm having is at if(persons[mid] < key) I get the error 'The < operator is undefined for the type'.
I thought it may have to do something with my compareTo method but I can't seem to fix it
Here is compareTo for reference
public int compareTo( Object o )
{
Person p = (Person) o;
int d = getLastName().compareTo(p.getLastName());
if (d == 0)
d = getFirstName().compareTo(p.getFirstName());
return d;
}
Thanks for any and all help!