title: Using Arrays.binarySearch on an array of Objects and looking for a String within the object
body: <p>OK so I have the following Object class:</p>
  
  <pre><code>public class IntoleranceFood implements Comparable&lt;IntoleranceFood&gt;{
  
  private int scoreInt;
  public String foodName;
  
  public IntoleranceFood(String food, int score) {
      super();
      this.foodName = food;
      this.scoreInt = score;
  
  }
  
  //getters and setters
  public String getFood() {
          return foodName;
  }
  public void setFood(String food) {
          this.foodName = food;
  }
  public int getScore() {
      return scoreInt;
  }
  public void setScore(int score) {
          this.scoreInt = score;
  }
  
  
  @Override
  public String toString() {
      return "Intolerance Food [Name=" + foodName + ", Score=" + scoreInt + "]";
  }
  
  @Override
  public int compareTo(IntoleranceFood arg0) {
  
      return toString().compareTo(arg0.toString());
  }
  
  }
  </code></pre>
  
  <p>And then in my Activity I have created an array for these objects to go into, and filled up the array with "IntoleranceFood" Objects:</p>
  
  <pre><code>int numFoodItemTypes = db.intoleranceFoodItemTypesTotal();
      IntoleranceFood[] foodArray = new IntoleranceFood[numFoodItemTypes];
  
      Cursor cAllFoodTypes = db.intoleranceFoodTypesList();
      int foodItem = 0;
      do{
          foodArray[foodItem] = new IntoleranceFood(cAllFoodTypes.getString(0), 0);
          foodItem++;
      }while(cAllFoodTypes.moveToNext());
  </code></pre>
  
  <p>I managed to sort the array by implementing Comparable and the compareTo method in my Object class:</p>
  
  <pre><code>Arrays.sort(foodArray);
  </code></pre>
  
  <p>But I want to then search the array using binary search, and look for the position in the array where a certain Object with a specific food name (String) resides. But I dont know how to get the following code working, and specifically in terms of:<br>
  -binarySearch(Object[] array, Object value)<br>
  I don't know what to put in "Object value" so this:</p>
  
  <pre><code>Arrays.binarySearch(foodArray, "Cereal");
  </code></pre>
  
  <p>Is clearly wrong! But I'm not sure how to search the Object array for an Object containing the String food name "Cereal".</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Yes so after the very useful reply below, I realsied what I need to be doing is:</p>
  
  <pre><code>IntoleranceFood searchOb = new IntoleranceFood("Cereal",0);
  
      int searchIndex = Arrays.binarySearch(foodArray, searchOb);
  </code></pre>
  
  <p>And that works!</p>