简体   繁体   中英

How can I read a text file of dates and convert them into an array that I can sort in C#?

I have a text file of dates that appear like this:

16/02/2015
13/02/2015
12/02/2015
11/02/2015
10/02/2015
09/02/2015

and so on.

How can I convert this into something that I can use in a quick sort for example? I am already reading the text file like this

string[] Date = System.IO.File.ReadAllLines("Date.txt");

I've tried using something like this:

double[] Dates = Array.ConvertAll(Date, s => DateTime.Parse(s));

And this just doesn't work.

My desired output after being put through my algorithm is to have them in order but outputting the same format as I have showed previously.

Any advice would be greatly appreciated. Thanks for reading.

EDIT

So I have managed to get the dates outputting in the way that I desire using this method:

string[] Date = System.IO.File.ReadAllLines("Date.txt");
DateTime[] Dates = Array.ConvertAll(Date, s => DateTime.Parse(s));

However, it is also outputting a time.

19/01/2014 00:00:00

How can I get rid of this?

Thanks again, guys!

DateTime.ParseExact takes a string and format to parse into a DateTime objects.

Using linq...

dates.Select(p=> DateTime.ParseExact(p, @"dd/MM/yyyy");

Should return a collection of dates you can then sort.

You can try below snippet,

 string[] Dates = File.ReadAllLines("Date.txt");
            var sortableDates = Dates
                .Select<string, DateTime>(d => DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture))
                .ToArray<DateTime>();

Updated Answer as per Edited question,

var sortableDates = File.ReadAllLines("Date.txt")
                .Select<string, DateTime>(d => DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture))
                .OrderBy<DateTime, DateTime>(d => d)
                .Select<DateTime, string>(d => d.ToString("dd/MM/yyyy"))
                .ToArray<string>();

Answer as per users implementation,

var result = Array.ConvertAll<string, DateTime>(Dates, d => DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture))
                 .OrderBy<DateTime, DateTime>(d => d)
                 .Select<DateTime, string>(d => d.ToString("dd/MM/yyyy"))
                 .ToArray<string>(); 

You can entirely avoid the issue of converting to a date doing it this way:

System.IO.File.ReadAllLines("Date.txt")
    .OrderBy(x => x.Substring(6, 4) + x.Substring(3, 2) + x.Substring(0, 2));

This only works because the data is uniform in each line.

int ConvertDate(string date)
    {
        string day = "";
        string month = "";
        string year = "";
        int count = 0;

        foreach(char c in date)
        {
            if(count < 2)
                day = day + c;
            else if(count > 5)
                year = year + c;
            else if(c != '/')
                month = month + c;

            count++;
        }

        string tempDate = year + month + day;
        return Convert.ToInt32(tempDate);
    }

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