// file: TitleComparator.java // author: Robert Keller // purpose: Comparator for use in sorting title triples in kwic application /** * Define functio object that compares two triples representing titles. * The second element of the triples is a list of words beginning with the * keyword. These elements determine the outcome of the comparison, which * is lexicographic ordering of the corresponding lower-case version of * the strings in the lists. */ class TitleComparator implements java.util.Comparator { /** * @return -1 if o1 is "less than" o2, +1 if "greater than, and 0 if "equal" */ public int compare(Object o1, Object o2) { OpenList L1 = (OpenList)((OpenList)o1).second(); OpenList L2 = (OpenList)((OpenList)o2).second(); while( L1.nonEmpty() && L2.nonEmpty() ) { String Word1 = (String)L1.first(); String Word2 = (String)L2.first(); int firstWordComparison = Word1.compareToIgnoreCase(Word2); if( firstWordComparison < 0 ) { return -1; } else if( firstWordComparison > 0 ) { return +1; } L1 = L1.rest(); L2 = L2.rest(); } if( L2.nonEmpty() ) return -1; if( L1.nonEmpty() ) return +1; return 0; } }