-1

I am setting up a list of date times:

DateTime a1
DateTime a2
DateTime a3
DateTime a4

The above looks like this (as DateTime objects):

3/1/2012 10:56
3/1/2012 17:03
3/1/2012 1:38
3/1/2012 5:33

Then I put them in a list and sort:

List<DateTime> ldtBites = new List<DateTime>();
ldtBites.Add(a1);
ldtBites.Add(a2);
ldtBites.Add(a3);
ldtBites.Add(a4);
ldtBites.Sort();

After Sorting I get this:

3/1/2012 1:38:00 AM
3/1/2012 10:56 AM
3/1/2012 5:03:00 PM
3/1/2012 5:33:00 AM
2
  • 2
    I cannot reproduce this. Running the code you post above sorts the dates properly. At first glance it appears to be sorting alphabetically. How are you outputting the list?
    – D Stanley
    Mar 1, 2012 at 22:21
  • @Shannon: are you certain that the last result is actually 3/1? When I tested your code, I made a mistake that caused the results to come out in your order, except that the last row was 3/2/2012 instead. See below.
    – Adam V
    Mar 1, 2012 at 22:37

4 Answers 4

2

You omitted the definition of w,x,y,z. I defined them as such:

DateTime w = new DateTime(2012, 3, 1, 10, 56, 0);
DateTime x = new DateTime(2012, 3, 1, 17, 3, 0);
DateTime y = new DateTime(2012, 3, 1, 1, 38, 0);
DateTime z = new DateTime(2012, 2, 29, 17, 3, 0);

This causes them to match your values for a1-a4; however, when I run the rest of your code, they sort correctly (a3, a4, a1, a2).

However, I noticed that x and z were the same hour/minute, so my initial test had this:

DateTime z = new DateTime(2012, 3, 1, 17, 3, 0);

When I ran this, I got them to come out in the order you were showing (a3, a1, a2, a4); however, after the AddHours() call went through, the z value was actually 3/2/2012, which is why it was last.

1
  • Adam you were right, instead of looking at my faulty output it seems you actually ran my code and caught my mistake of looking at code for 5 hours straight...thanks!
    – Shannon
    Mar 1, 2012 at 23:03
1

You don't want to convert back and forth. Just do it once. Sort your list first, and only then convert to string.

0

Converting to string and converting back might cause that result. Why don't you add x,y,w,z to your list directly?

List<DateTime> ldtBites = new List<DateTime>();
ldtBites.Add(DateTime.Parse("3/1/2012 10:56"));
ldtBites.Add(DateTime.Parse("3/1/2012 17:03"));
ldtBites.Add(DateTime.Parse("3/1/2012 1:38"));
ldtBites.Add(DateTime.Parse("3/1/2012 5:33"));
ldtBites.Sort();

foreach (DateTime dt in ldtBites)
    Console.WriteLine(dt);

Output:

3/1/2012 1:38:00 AM

3/1/2012 5:33:00 AM

3/1/2012 10:56:00 AM

3/1/2012 5:03:00 PM

Press any key to continue . . .

0
-1

Above will work only if all the date are same, in case the date are also different you should do the following...

var sortedDates = dates.OrderByDescending(x => x);

or else Don't want to use, or don't know Linq then you can go for following..

static List SortAscending(List list)
{
list.Sort((a, b) => a.CompareTo(b));
return list;
}

static List SortDescending(List list)
{
list.Sort((a, b) => b.CompareTo(a));
return list;
}

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.