Mr F
Mr F

Reputation: 63

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!

Upvotes: 3

Views: 1255

Answers (4)

Enigmativity
Enigmativity

Reputation: 117010

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.

Upvotes: 0

Ashish Sapkale
Ashish Sapkale

Reputation: 550

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>(); 

Upvotes: 1

kidshaw
kidshaw

Reputation: 3451

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.

Upvotes: 4

Nick Jennett
Nick Jennett

Reputation: 11

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);
    }

Upvotes: -1

Related Questions