Jeffrey,
Between this and the other thread addressing this issue, I feel like my suggestions have been falling on deaf ears. You have yet to respond to my inquiries as to why you are storing date values as strings instead of the appropriate DateTime data type. Perhaps this sample and explanation will help to convince you that the problems you are having are the result of an incorrect choice in data type usage.
Consider the following code:
Code:
string[] strTimes = new string[] { "8:30", "08:40", "9:00", "10:00" };
for(int i = 0; i < strTimes.Length - 1; i++)
{
Console.Write("{0} <-> {1}:", strTimes[i], strTimes[i+1]);
Console.Write("[string: {0}]", string.Compare(strTimes[i], strTimes[i + 1]));
Console.Write("[date: {0}]", DateTime.Compare(DateTime.Parse(strTimes[i]), DateTime.Parse(strTimes[i + 1])));
Console.WriteLine();
}
When I run this, I get the following output:
Code:
8:30 <-> 08:40:[string: 1][date: -1]
08:40 <-> 9:00:[string: -1][date: -1]
9:00 <-> 10:00:[string: 1][date: -1]
Explanation of actual comparison results:
8:30 <-> 08:40
- As strings: first is greater (incorrect)
- As datetimes: second is greater (correct)
08:40 <-> 9:00
- As strings: second is greater (correct)
- As datetimes: second is greater (correct)
9:00 <-> 10:00
- As strings: second is greater (correct)
- As datetimes: first is greater (incorrect)
Why the incorrect ones?
8:30 <-> 08:40
- As strings, '0' comes before '8'.
- As datetimes, clearly 8:30 is before 8:40
9:00 <-> 10:00
- As strings: '1' comes before '9'
- As datetimes: 9:00 comes before 10:00
What this all means is that doing a comparison between strings and dates will not yield the same result. This means that your query and/or code that uses string comparisons will have unexpected results.
It bothers me to see someone spending any amount of time trying to debug a problem that shouldn't even exist. The simple fact is that you need to change the data type you are using for these values otherwise you are going to have problems.
I am confident that if you change your data types, all your comparison problems will disappear.
-Peter