I have a sorted array of strings.
Given a string that identifies a prefix, I perform two binary searches to find the first and last positions in the array that contain words that start with that prefix:
string [] words = {"aaa","abc","abcd","acd"};
string prefix = "abc";
int firstPosition = Array.BinarySearch<string>(words, prefix);
int lastPosition = Array.BinarySearch<string>(words, prefix + char.MaxValue);
if (firstPosition < 0)
firstPosition = ~firstPosition;
if (lastPosition < 0)
lastPosition = ~lastPosition;
Running this code I get firstPosition and lastPosition both equal to 1, while the right answer is to have lastPosition equal to 3 (i.e., pointing to the first non-matching word).
The BinarySearch method uses the CompareTo method to compare the objects and I have found that
("abc"+char.MaxValue).CompareTo("abc")==0
meaning that the two string are considered equal!
If I change the code with
int lastPosition = Array.BinarySearch<string>(words, prefix + "z");
I get the right answer.
Moreover I have found that
("abc"+char.MaxValue)==("abc")
correctly (with respect to my needs) returns false.
Could you please help me explaining the behavior of the CompareTo method?
I would like to have the CompareTo method to behave like the ==, so that the BinarySearch method returns 3 for lastPosition.
According to the MSDN,
string.CompareToshould not be used to check whether two strings are equal:To get the behavior you wish, you could make use of the overload that accepts an
IComparer<T>:This will return
-4forlastPositionas there is no string with that prefix in the array. I don’t understand why you expect3in that case…