Only one comparator
Do not use two comparators, use one comparator that compares both values:
public int compare(Foo a, Foo b){ // compare bar() values first int result = a.bar().compareTo(b.bar()); // compare baz() values only if bar() values are different if(result==0){ result = a.baz().compareTo(b.baz()); } return result; }
(In your case, bar () is the name, and baz () is the number).
Use libraries
Creating comparators this way is a lot easier if you use Guava or Commons / Lang
Guava Versions:
@Override public int compare(final Foo a, final Foo b){ return ComparisonChain .start() .compare(a.bar(), b.bar()) .compare(a.baz(), b.baz()) .result(); }
Commons / Lang Version:
@Override public int compare(final Foo a, final Foo b){ return new CompareToBuilder() .append(a.bar(), b.bar()) .append(a.baz(), b.baz()) .toComparison(); }
(Both of these versions will not fail if either of the values ββis null, my quick and dirty code above will be)
Solve the problem
I donβt think you should do a binary search first, it seems very complicated.
Why don't you use TreeSet with a custom comparator ? Or Collections.sort(list, comparator) ? (For both of these parameters, you can use the comparators that I showed earlier).
In addition, you should consider allowing your contestant to implement Comparable<Contestant> . This way you do not need to use an external Comparator . You can use the same logic as above in the compareTo() method, just replace one of the objects with this .