I have a
DateTime class (my own, not
You could have just pasted the code for that class. Since you are asking a question about sorting, and your
DateTime class implements
IComparable, its content could have been pivotal for a proper review.
Here is a method that takes a
List<DateTime> and sorts it:
You have just told us your class implements
IComparable, but you don't show us how you'd use its functionality. I would have expected to to see
dateTimes[i].CompareTo(dateTimes[j]) but instead we get
dateTimes[i] < dateTimes[j]. This means you have created an operator overload on
< and are not sharing this crucial code with us.
Furthermore, why would you roll out your own sorting algorithm if built-in functionality is available? As suggested in the comments by VisualMelon, you could just call
I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy
To get things even more confusing, you ask about a syntactic change to the algorithm rather than going about how to properly sort or how to correctly implement
Finishing off, in the
for (dateTime1 in dateTimes) approach, you cannot change
dateTimes[i] = dateTimes[j]; as it will
throw System.InvalidOperationException: Collection was modified; enumeration operation may not execute. rendering your suggested approach unusable.
As a challenge, I let you search how to prevent this exception.