Sorting a String List by a date

给你一囗甜甜゛ 提交于 2020-03-05 07:06:09

问题


I'm looking to sort a String List by date. The string I receive is in the form displayed below:

  • 1-05-2017,Early May bank holiday,scotland
  • 1-08-2016,Summer bankholiday,scotland
  • 1-12-2014,St Andrew's Day,scotland
  • 14-04-2017,Good Friday,england-and-wales

Is there a way to get the date, even though the sub-string is variable in length, without appending a 0 to the initial number if it's a single digit? I've tried using Regex.Match() with a but can't seem to work out the best way to handle the first few digits:

Match match = Regex.Match(toBeInsertedList.ToString(), @"\d{1}-\d{2}-\d{4}");
string date = match.Value;
var combineList = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.CurrentCulture);

I've also tried the below code in its many forms:

var orderedList = toBeInsertedList.OrderByDescending(x => DateTime.Parse(x)).ToList();

Any guidance would be good, as I'm still learning.

Thanks!


回答1:


A different idea is to use string.Split(',') to split your string on the comma character and grab the first item from the resulting array as the date string, and then use DateTime.ParseExact() to parse the string into a valid date:

static void Main()
{
    var lines = new List<string>
    {
        "1-05-2017,Early May bank holiday, scotland",
        "1-08-2016,Summer bankholiday, scotland",
        "1-12-2014,St Andrew's Day,scotland",
        "14-04-2017,Good Friday, england-and-wales"
    };

    var orderedLines = lines.OrderByDescending(x => 
        DateTime.ParseExact(x.Split(',')[0], "d-MM-yyyy", 
            CultureInfo.InvariantCulture)).ToList();

    foreach (var line in orderedLines)
    {
        Console.WriteLine(line);
    }

    Console.CursorVisible = false;
    Console.Write("\nPress any key to exit...");
    Console.ReadKey();
}

Note that the result is printing out in my local culture format:




回答2:


It's a simple change to your regex to extract that initial digit or 2 digits:

\d{1,2}-\d{2}-\d{4}

This simply says grab between 1 and 2 digits before the first dash.




回答3:


There are two points here

Firstly, you can make specify the number of character by using {1,2} which means 1 or 2 characters are matched.

\d{1,2}-\d{2}-\d{4}

Secondly, the format is wrong

var combineList = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.CurrentCulture);

should be as below, where "d" means the minimum number of the digit is one, while "dd" means the minimum is two.

var combineList = DateTime.ParseExact(date, "d-MM-yyyy", CultureInfo.CurrentCulture);

Below is the code I use to test

var str = @"1-05-2017,Early May bank holiday,scotland
1-08-2016,Summer bankholiday, scotland
1-12-2014,St Andrew's Day,scotland
14-04-2017,Good Friday, england-and - wales";

var matches = Regex.Matches(str, @"\d{1,2}-\d{2}-\d{4}");
foreach (var match in matches)
{
    string date = match.ToString();
    var combineList = DateTime.ParseExact(date, "d-MM-yyyy", CultureInfo.CurrentCulture);
    Debug.WriteLine(combineList.ToString());
}

Result

5/1/2017 12:00:00 AM
8/1/2016 12:00:00 AM
12/1/2014 12:00:00 AM
4/14/2017 12:00:00 AM


来源:https://stackoverflow.com/questions/48493455/sorting-a-string-list-by-a-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!