Link to home
Create AccountLog in
Avatar of gregalhaig1
gregalhaig1

asked on

Array.BinarySearch returning negative number in a sorted array, but only in one geographical location out of three

We are running into a problem with the Array.BinarySearch in our C# app.

We have a custom program deployed over three different geographical locations, and this problem only occurs at one of the locations. All locations call the same methods and have the same data returned to them.

Here is an abbreviation of the method that is causing the problem:

        private void ViewableRange(DateTime startDate, DateTime endDate, ScheduleDay[] days)
        {
            int startDayIndex = Array.BinarySearch(days, new ScheduleDay(startDate));
            int endDayIndex = Array.BinarySearch(days, new ScheduleDay(endDate));
        }


ScheduleDay is  class that contains a date and a bool that indicates if the date is a working day. This array contains 853 items, and the date of the first item in the array is 12/3/2012. The date of the last item in the array is 4/4/2015.

The startDate and endDate are the date range we want to view (2/12/2013 and 3/11/2013 respectivly).

The startDay index should be the index of the date in the array that matches the startDate, and the endDateIndex should be the date in the array that matches the endDate.

In geographical locations A and B, this code functions correctly and returns the following:
startDayIndex = 71
endDayIndex = 98

In geographical location C, it returns the following:
startDayIndex = -72
endDayIndex = -99

I can confirm the following for all locations:
The array is sorted
The array contains both the startDate and the endDate
The array contains all dates inclusive from 12/3/2012 through 4/4/2015.


Any ideas why this code is not performing correctly in one geographical location, but works fine in the other two? Is there an environmental variable that would affect the performance of the Array.BinarySearch? (ex: if the computers at their location recognize Monday as the first day of the week, and the computers at the other locations recognize Sunday…I tried changing my local machine date variables many different ways, but had no luck.)

PS – I and my development machine are at location A (US). Location B is in Canada, and location C is in the US.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gregalhaig1
gregalhaig1

ASKER

kaufmed,

Yes, ScheduleDay implements the IComparable interface. The comparison is made using Date.

public int CompareTo(object obj)
        {
            return Date.CompareTo(((ScheduleDay)obj).Date);
        }
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thanks for point us in the right direction kaufmed!
It fixed the issue.