user6234613
user6234613

Reputation: 165

LINQ unable to OrderBy date in specific column?

I have a CSV file which goes like this:

1,01/01/2001,a
2,19/09/2013,as
3,12/05/2016,asd
4,13/05/2016,asdf
5,12/12/2012,asdfg
6,05/02/2006,asdfgh
7,06/03/2011,asdfghj

I want to sort and display the dates in chronological order but I can't seem to get my code to sort it out. However, my code is able to display the dates according to the format in the CSV file.

If the CSV file is like this:

01/01/2001
19/09/2013
12/05/2016
13/05/2016
12/12/2012
05/02/2006
06/03/2011

Then, it works just fine.

The following is my code:

string parts = new StreamReader(@"C:\input.txt").ReadToEnd();
string[] file = parts.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
StringBuilder sb = new StringBuilder();

for (int i = 0; i < file.Length; i++)
{
    string[] data = file[i].Split(new string[] { "," }, StringSplitOptions.None);
    string[] dates = new string[] { data[1].ToString() };
    //var date = dates.OrderBy(x => DateTime.Parse(x)).ToList();
    var date = dates.OrderBy(x => DateTime.ParseExact(x, "dd/MM/yyyy", null));

    foreach (string s in date)
    {
        sb.AppendLine(s + "<br />");
        Label1.Text = sb.ToString();
    }
}

I have tested this code using both DateTime.Parse and DateTime.ParseExact on the CSV file, but they didn't work.

I'm really new to LINQ query and this is my first time using it. I would really appreciate it if there is an explanation provided on what's wrong with my code.

Upvotes: 1

Views: 886

Answers (3)

desflan
desflan

Reputation: 468

Please first put your dates into a Collection, for example a List or Array, and then sort the Collection. Default sort order is ascending

        List<string> unsortedDates = new List<string>();

        string parts = new StreamReader(@"C:\input.txt").ReadToEnd();
        string[] file = parts.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < file.Length; i++)
        {
            string[] data = file[i].Split(new string[] { "," }, StringSplitOptions.None);
            unsortedDates.Add(data[1]);
        }

        var sortedDatesAscending = unsortedDates.OrderBy(x => DateTime.ParseExact(x, "dd/MM/yyyy", null));

        foreach (string s in sortedDatesAscending)
        {
            sb.AppendLine(s + "<br />");
            Label1.Text = sb.ToString();
        }

Upvotes: 0

filhit
filhit

Reputation: 2154

You are sorting an array (dates) that always contains one date (because you create it each time from a single line in a loop). Then you add this line to the label's text.

You have to create a collection that contains all the dates, before you sort it.

string parts = new StringReader(@"C:\input.txt").ReadToEnd();
string[] lines = parts.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
StringBuilder sb = new StringBuilder();
List<string> dates = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
    string[] data = lines[i].Split(new string[] { "," }, StringSplitOptions.None);
    dates.Add(data[1]);
}

var datesSorted = dates.OrderBy(x => DateTime.ParseExact(x, "dd/MM/yyyy", null));
foreach (string s in datesSorted)
{
    sb.AppendLine(s + "<br />");
}

Label1.Text = sb.ToString();

Upvotes: 3

Muhammed Shevil KP
Muhammed Shevil KP

Reputation: 1396

It may required to select the values in Date format first then order it Try

var date = dates.Select(x => DateTime.ParseExact(x, "dd/MM/yyyy", null)).OrderBy(t => t).ToList();
foreach (DateTime s in date)
{
     sb.AppendLine(s.ToString("dd/MM/yyyy") + "<br />");
}
Label1.Text = sb.ToString();

Need to move the Label1 Text assigning after foreach

Upvotes: 1

Related Questions