17

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?

Muntasir
  • 798
  • 1
  • 14
  • 24

6 Answers6

50

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);
mipe34
  • 5,596
  • 3
  • 26
  • 38
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);
Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27
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));
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
1

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

List<DateTime> dates = ...

dates.OrderByDescending(c => c).ToList();
spajce
  • 7,044
  • 5
  • 29
  • 44
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..

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
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.

Bravo Yeung
  • 8,654
  • 5
  • 38
  • 45