简体   繁体   English

按日期对字符串列表进行排序

[英]Sorting a String List by a date

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-05-2017,五月初的银行假期,苏格兰
  • 1-08-2016,Summer bankholiday,scotland 2016年1月1日-夏季银行假日,苏格兰
  • 1-12-2014,St Andrew's Day,scotland 2014年1月12日,苏格兰圣安德鲁节
  • 14-04-2017,Good Friday,england-and-wales 2017年4月14日,耶稣受难日,英格兰和威尔士

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? 即使子字符串的长度可变,是否有一种获取日期的方法,如果它是一位数字的话,也不会在初始数字后附加0? I've tried using Regex.Match() with a but can't seem to work out the best way to handle the first few digits: 我尝试过使用带有Regex.Match()的方法 ,但似乎无法找出处理前几位数的最佳方法:

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! 谢谢!

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. 这只是说在第一个破折号之前抓住1到2位数字。

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. 首先,您可以使用{1,2}来指定字符数,这表示匹配1或2个字符。

\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. 应如下所示,其中“ d”表示数字的最小值为1,而“ dd”表示数字的最小值为2。

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

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: 一个不同的想法是使用string.Split(',')在逗号字符上分割字符串,并从结果数组中获取第一项作为日期字符串,然后使用DateTime.ParseExact()将字符串解析为有效日期:

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: 请注意,结果是以我的本地文化格式打印出来的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM