What would be the best way to do a binary search in an array that you can actually access it via indirection?
Meaning I have an Integer[] that stores the indexes of a String[] that represent the sorted version of the String[]
E.g. Integer[] idxes= {5, 4, 0, 3 , 1, 2} which means that the String[5] or String[idxes[0]] is the lowest string lexicographically.
I hope the description is clear.
What would be the best way to do a binary search via the idexes for a String if it is part of the String[] words ?
What I did is the following:
int pos = Arrays.binarySearch(idexes, -1, new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
if(o1 == -1){
return k.compareTo(words[o2]);
}
else{
return words[o1].compareTo(k);
}
}
});
where k is the search word and words[] is the String[] I mentioned earlier.
This works, but I don't like the -1 I pass in the binarySearch api.
Is there a better way to approach this?