简体   繁体   中英

Sort text file array by date in C#

I have a text file imported and stored in a string array which looks like:

firstname, lastname, middlename, D/O/B, gender    
firstname, lastname, middlename, D/O/B, gender    
firstname, lastname, middlename, D/O/B, gender

etc etc etc

I want to sort the file by the DOB (studentFile[3]) but cannot get the app to read the array by datetime. This is what I have so far

var dateOrder = studentFile.OrderByDescending(x => DateTime.Parse(x.Split(',')[4]));
foreach (var date in dateOrder)
{
    Console.WriteLine("\t" + date);
}

Any ideas on where I'm going wrong and how to correct it?

x.Split(',')结果的索引应该是3,而不是4

First, put your index to 3. Second, try to explicitly decalre your datetime array:

DateTime[] dateOrder = studentFile.OrderByDescending(x => DateTime.Parse(x.Split(',')[3]));
foreach (DateTime date in dateOrder)
    {
         Console.WriteLine("\t" + date.ToString());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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