BinarySearch did not expect results

I filled out a sorted list of dates (stored as strings in dd / mm / yyyy format) from an xml file using xpath.

However, when I query the list to see if a date exists in the list, I always get a negative result (i.e. does not exist), even if I have a hardcode, the query string matches the date in the list.

However, when comparing strings by index containing the requested string, I get 0 indicating that the strings are identical.

What can cause this odd behavior?

As requested, here is the code

Holidays are filled:

while (iter.MoveNext())
{
    XPathNavigator nav2 = iter.Current;
    XPathNodeIterator iter2 = nav2.SelectDescendants("date", "", false);
    while (iter2.MoveNext())
    {
        date = iter2.Current.ToString();
        holidays.Add(date);
    }
}

return holidays;

And the search:

holidays = list.getHolidays();
if(holidays.BinarySearch(DateTime.Now.ToShortDateString()) > 0)

When returning the following XML:

<date>01/01/2009</date>
<date>25/02/2009</date>
<date>10/04/2009</date>
<date>13/04/2009</date>
<date>04/05/2009</date>
<date>25/05/2009</date>
<date>31/08/2009</date>
<date>25/12/2009</date>
<date>28/12/2009</date>
+3
source share
1 answer

, . , , , .

:

//

, .

( DateTime), , , , , .

+10

Source: https://habr.com/ru/post/1703822/


All Articles