21

I have a List<string> with dates.
My list is:

{"01/01/2013","10/01/2013","20/01/2013"}

I want to sort the list to look like this:

{"20/01/2013","10/01/2013","01/01/2013"}

How can I make this?

6 Answers 6

57

With linq:

var list = new List<string> {"01/01/2013", "10/01/2013", "20/01/2013"};
var orderedList = list.OrderByDescending(x => DateTime.Parse(x)).ToList();

UPDATE (according questions in comments):

You can treat invalid dates like this (invalid date is there treated as default(DateTime)):

var list = new List<string> { "01/01/2013", "10/01/2013", "N/A" , "20/01/2013"  };
var orderedList2 = list.OrderByDescending(x =>
            {
                DateTime dt;
                DateTime.TryParse(x, out dt);
                return dt;
            });

Or if you want to have invalid datetime as first item in the list:

var orderedList3 = list.OrderByDescending(x =>
            {
                DateTime dt;
                if (!DateTime.TryParse(x, out dt)) return DateTime.MaxValue;
                return dt;
            }); 

You can also filter the invalid dates out:

var filteredList = list.Where(x =>
            {
                DateTime dt;
                return DateTime.TryParse(x, out dt);
            }).Select(DateTime.Parse).OrderByDescending(x => x);

Or even better:

var filteredList = list.Select(x =>
        {
            DateTime dt;
            return new {valid = DateTime.TryParse(x, out dt), date = dt};
        }).Where(x => x.valid).Select(x => x.date).OrderByDescending(x => x);
Sign up to request clarification or add additional context in comments.

2 Comments

How about if there is an invalid datetime string? Is there any way to alter that invalid datetime with a specific string?
What if there are strings as well? Like missing? how do you sort
4

You shouldn't use string representations of data - we're all living in object-oriented world :)

Best way would be to convert these strings into actual DateTime objects and sort them in reverse order via linq:

var dates = Array.ConvertAll(dateStrings, x => DateTime.Parse(x));
return dates.OrderByDesc(x => x);

Another way would be to implement custom sorting function, see this link. Then you'd just use it in a sort function:

DateAsStringComparer myComparer = new DateAsStringComparer();
dateStrings.Sort(myComparer);

2 Comments

+1 because you didn't bother with the string comparison like I did :)
actually, you don't need the lambda, you can do it like this -Array.ConvertAll(dateStrings,DateTime.Parse)
4

Try this:

List<string> s = new List<string>() { "01/01/2013", "10/01/2013", "20/01/2013" };
var d = s.OrderByDescending(i => DateTime.ParseExact(i, "dd/MM/yyyy", null));

Comments

1

Why do you want to use List<string> instead of List<DateTime>?

List<DateTime> dates = ...

dates.OrderByDescending(c => c).ToList();

1 Comment

I'm sure he had his reasons. I need it as well, for example.
0

Because they are the UK/AUS format (Day/Month/Year), you can sort them using OrderByDescending:

List<string> dates = new List<string>() { "01/01/2013", "10/01/2013", "20/10/2013" };

foreach (var date in dates.OrderByDescending(x => x))
    Console.WriteLine(date);

Personally I would convert them to DateTime objects first..

Comments

0

To convert DateTime string of custom format to string of unified format, you can adopt following simple and safe snippet.

    string formatStr = "yyyyMMddHHmmss";
    string result = Convert.ToDateTime(inputStr).ToString(formatStr);

Inside the code, formatStr can be any possible forms the DateTime class can accept, you can set it as the format you need, here you can just use dd/MM/yyyy which is matched with your target format string such as "20/01/2013".

So for your case, the code can be simple as below:

    List<string> list = new List<string> { "01/01/2013", "10/01/2013", "20/01/2013" };
    var listAfterSorting = list.OrderByDescending(t => 
        Convert.ToDateTime(t).ToString("dd/MM/yyyy")
            .ToList());

While in some cases, using ParseExact to parse a data time string, it will throws error/exception String was not recognized as a valid DateTime, in that case if you turn to use TryParseExact, the result is probably default(DateTime)= 1/1/0001 12:00:00 AM because of parsing failed. Therefore, I do not recommend ParseExact or TryParseExact.

Comments

Your Answer

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