How can I Order CSV file using Microsoft.VisualBasic.FileIO.TextFieldParser - Ascending Descending

362 views Asked by At

Inside of my file i have:

        2,Sam,500.00
        6,Mike,400.00
        8,Robert,156.00
        3,Steve,100.85
        9,Anderson,234.90

Order: ID, Name, Salary.

I am working on a CSV parser using C# TextFieldParser.

            using (TextFieldParser parser = new TextFieldParser(path))
            {
                parser.TextFieldType = FieldType.Delimited;

And i use a Delimiter by comma

                parser.SetDelimiters(",");

                bool firstLine = true;

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();

                    if (firstLine)
                    {
                        foreach (var val in fields)
                        {
                            dt.Columns.Add(val);
                        }
                        firstLine = false;
                        continue;
                    }
                    dt.Rows.Add(fields);

How can i Sort the Data inside? Using Linq without using the function Split

   var people = from line in File.ReadLines(path)
         let parts = line.Split(delimiter)
         select new Person {
            Id = Int32.Parse(parts[0]),
            Name = parts[1],
            Salary = Decimal.Parse(parts[2])
         };

Many Thanks

0

There are 0 answers