jumbojs
jumbojs

Reputation: 4868

C# lists - sorting date problem

I have a C# list collection that I'm trying to sort. The strings that I'm trying to sort are dates "10/19/2009","10/20/2009"...etc. The sort method on my list will sort the dates but the problem is when a day has one digit, like "10/2/2009". When this happens the order is off. It will go "10/19/2009","10/20/2009","11/10/2009","11/2/2009","11/21/2009"..etc. This is ordering them wrong because it sees the two as greater than the 1 in 10. How can I correct this?

thanks

Upvotes: 2

Views: 6811

Answers (7)

dgstl
dgstl

Reputation: 61

I wanted to see how well I could outperform Chris's solution with my own IComparer. The difference was negligible. To sort the same list of one million dates, my solution took 63.2 seconds, and Chris's took 66.2 seconds.

/// <summary>
/// Date strings must be in the format [M]M/[D]D/YYYY
/// </summary>
class DateStringComparer : IComparer<string>
{
   private static char[] slash = { '/' };

   public int Compare(string Date1, string Date2)
   {
      // get date component strings
      string[] strings1 = Date1.Split(slash);
      string[] strings2 = Date2.Split(slash);

      // get date component numbers
      int[] values1 = { Convert.ToInt32(strings1[0]),
                         Convert.ToInt32(strings1[1]),
                         Convert.ToInt32(strings1[2]) };
      int[] values2 = { Convert.ToInt32(strings2[0]),
                         Convert.ToInt32(strings2[1]),
                         Convert.ToInt32(strings2[2]) };

      // compare year, month, day
      if (values1[2] == values2[2])
         if (values1[0] == values2[0])
            return values1[1].CompareTo(values2[1]);
         else
            return values1[0].CompareTo(values2[0]);
      else
         return values1[2].CompareTo(values2[2]);
   }
}

As for sorting the dates as pre-existing DateTime instances, that took 252 milliseconds.

Upvotes: 1

decasteljau
decasteljau

Reputation: 8043

If you care about performance and if that is possible for you, you would preferably sort your dates before you generate the strings. You would then use the date objects directly for the sort.

You would then save time manipulating strings back and forth.

Upvotes: 0

Chris Hynes
Chris Hynes

Reputation: 10239

The problem is they're strings, but you want to sort them by dates. Use a comparison function that converts them to dates before comparing. Something like this:

List<string> strings = new List<string>();

// TODO: fill the list

strings.Sort((x, y) => DateTime.Parse(x).CompareTo(DateTime.Parse(y)));

Upvotes: 17

Bob Nadler
Bob Nadler

Reputation: 2765

Parse the strings to DateTime objects and use DateTime.Compare.

Chris beat me to it!

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

Assuming all your strings will parse:

MyList.OrderBy(d => DateTime.Parse(d));

Otherwise, you might need to use ParseExact() or something a little more complicated.

Upvotes: 4

John Fisher
John Fisher

Reputation: 22717

You need to either use a sort specific for dates, or use something like Natural Sort.

Upvotes: 0

Henry Gao
Henry Gao

Reputation: 4936

write a compare method to convert "10/2/2009" to a date then compare

Upvotes: 1

Related Questions