import java.util.*;

class AlphabeticComparator implements Comparator<String> {  
  public int compare(String s1, String s2) {   
    // make case irrelevant
    return s1.toLowerCase().compareTo(s2.toLowerCase());  
  }
}

/**
 * Demonstrates searching an array of strings that are
 * in alphabetical order (as imposed by the AlphabeticComparator)
 *
 * @author Mark Hall
 * @version 1.0
 */
public class AlphabeticSearch {
  public static void main(String [] args) {
    String [] a = new String [] {"Fred", "george", "Bob",
                                 "Zaphod", "Mary", "sue"};
    AlphabeticComparator comp = new AlphabeticComparator();
    String searchItem = new String(a[1]);
    Arrays.sort(a, comp); //sort with respect to the Comparator

    // search for an element that is already in the array
    int index = Arrays.binarySearch(a, searchItem, comp);
    System.out.println("Index = " + index);

    // search for something that is not present
    index = Arrays.binarySearch(a, "Craig", comp);
    System.out.println("Index = " + index);
  }
}
