Sort string list with dates in C#

前端 未结 6 1594
不知归路
不知归路 2020-12-31 07:00

I have a List with dates.
My list is:

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

I want to sort the list

相关标签:
6条回答
  • 2020-12-31 07:42

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

    List<DateTime> dates = ...
    
    dates.OrderByDescending(c => c).ToList();
    
    0 讨论(0)
  • 2020-12-31 07:49

    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);
    
    0 讨论(0)
  • 2020-12-31 07:58

    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));
    
    0 讨论(0)
  • 2020-12-31 08:00

    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);
    
    0 讨论(0)
  • 2020-12-31 08:02

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

    0 讨论(0)
  • 2020-12-31 08:05

    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.

    0 讨论(0)
提交回复
热议问题