1
\$\begingroup\$

I have a DateTime class (my own, not System.DateTime) which implements the IComparable interface.

Here is a method that takes a List<DateTime> and sorts it:

private List<DateTime> SortDateTimes(List<DateTime> dateTimes)
{
    for (int i = 0; i < dateTimes.Count; i++)
    {
        for (int j = 0; j < dateTimes.Count; j++)
        {
            if (dateTimes[i] < dateTimes[j])
            {
                var temp = dateTimes[i];
                dateTimes[i] = dateTimes[j];
                dateTimes[j] = temp;
            }
        }
    }

    return dateTimes;
}

I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy. :D

foreach(var dateTime1 in dateTimes)
{
    foreach(var dateTime2 in datetimes)
    ...

What are your thoughts? Thanks!

\$\endgroup\$
7
  • 8
    \$\begingroup\$ This doesn't have a 'reinventing the wheel tag'...: have you considered using List.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will take n^2 steps to sort your list, where n is the number of items in the list. An 'efficient' sorting algorithm (as List.Sort will use) will do the same job in n log(n) steps, which is big difference for a large n. \$\endgroup\$ Commented Feb 5, 2018 at 18:24
  • \$\begingroup\$ Hmm, as said above: stackoverflow.com/questions/26868600/… \$\endgroup\$ Commented Feb 5, 2018 at 18:25
  • \$\begingroup\$ @VisualMelon so all I need to do is dateTimes.Sort()? \$\endgroup\$ Commented Feb 5, 2018 at 18:27
  • 3
    \$\begingroup\$ I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks for List<T>.Sort which details how the DefaultComparer<T> is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception. \$\endgroup\$ Commented Feb 5, 2018 at 18:37
  • 2
    \$\begingroup\$ Splitting hairs - I'd call the DateTime class something else - just about anything else. Obviously two types can have the same name, but it's not worth even the moment of confusion caused by using the same name as such a common type. \$\endgroup\$ Commented Feb 7, 2018 at 1:53

1 Answer 1

2
\$\begingroup\$

Prologue

I have a DateTime class (my own, not System.DateTime)

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.

Parodos

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 dateTimes.Sort();

Exodus

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 IComparable.

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.

Cliffhanger

As a challenge, I let you search how to prevent this exception.

\$\endgroup\$
7
  • \$\begingroup\$ You've got downvoted! I wonder why... and the quesiton lost its two upvotes... one was from me the the other one is gone too. This is so weird o_O I'm not sure... is this question off-topic or not... strage things happen today. \$\endgroup\$
    – t3chb0t
    Commented Aug 26, 2019 at 17:39
  • \$\begingroup\$ @t3chb0t Perhaps it's off-topic, it's definately a border-line one, but it wasn't deemed off-topic back then. My main goal here was to confront OP with their own code and phrasing to make them reflect their design. \$\endgroup\$
    – dfhwze
    Commented Aug 26, 2019 at 17:46
  • \$\begingroup\$ It seems to be more of a criticism about how I wrote the question rather than an answer. I believe you should write all of this in a comment. \$\endgroup\$ Commented Aug 27, 2019 at 11:24
  • \$\begingroup\$ @Sipo, it is to some extend criticism on the question, but at the end I also review your proposed solution. Would you like me to withdraw this answer or is it fine for you? \$\endgroup\$
    – dfhwze
    Commented Aug 27, 2019 at 11:28
  • 1
    \$\begingroup\$ Also, this question was asked 1.5 years ago, and it is hard for me to improve it because I do not remember the details. \$\endgroup\$ Commented Aug 27, 2019 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.