-2

I have seen many subjects on sorting a tab delimited but have been unable to either grasp or comprehend the task to finish this piece of code plus I am just starting in C#. I hope someone can answer this question.

I want to open a text file which is tab delimited with a specific amount of fields. The questions is how do I sort it with the first column field then using a second column. I would like to be able to see the fields in a list array, for the debugging, if that is possible. I hope this sample comes across as tab delimited. Then of course I want to be able to write it back.

Category Name   Category Sub Name   Family  Sales Description   Equipment Tag   List Price  Price   ID
Fixture Type 2  Basket  Sales   B2  65  64  366589
Fixture Type 2  Basket  Sales   B2  65  64  366595
Fixture Type 2  Basket  Sales   B2  65  64  366601
Fixture Type 2  Basket  Sales   B2  65  64  366607
Fixture Type 2  Basket  Sales   B2  65  64  366613
Fixture Type 22 Rail    Sales   X1  10  10  382822
Device  Type 1  Wall    Outside Null    360 342 400604
Device  Type 3  Standard    Outside Null    180 171 400885
Device  Type 1  Wall    Outside Null    360 342 400965
Device  Type 1  Wall    Outside Null    360 342 401034
Device  Type 1  Wall    Outside Null    360 342 401303
Device  Type 3  Standard    Standard    Null    180 171 401471
Device  Type 1  Wall    Outside Null    360 342 401596
Device  Type 3  Standard    Standard    Null    180 171 401753
Device  Type 3  Standard    Standard    Null    180 171 401866
Device  Type 1  Wall    Outside Null    360 342 402189
Device  Type 3  Standard    Standard    Null    180 171 402537
Device  Type 1  Wall    Outside Null    360 342 402685
Device  Type 1  Wall    Outside Null    360 342 402930
Device  Type 1  Wall    Outside Null    360 342 402952
Device  Type 3  Standard    Standard    Null    180 171 403164
Device  Type 1  Wall    Outside Null    360 342 403234
Device  Type 3  Standard    Standard    Null    180 171 403303
Device  Type 1  Wall    Outside Null    360 342 403473
Fixture Type 4  Standard    Null    F1  140 137 406101
Fixture Type 4  Step    Null    F1  140 137 406102
Fixture Type 4  Step    Null    F1  140 137 406103
Fixture Type 4  Step    Null    F1  140 137 406104
Fixture Type 4  Step    Null    F1  140 137 406105
Fixture Type 4  Step    Null    F1  140 137 406106
Fixture Type 4  Step    Null    F1  140 137 406124
Fixture Type 4  Step    Null    F1  140 137 406125
Fixture Type 4  Step    Null    F1  140 137 406126
Fixture Type 4  Step    Null    F1  140 137 406127
Fixture Type 4  Step    Null    F1  140 137 406128
Fixture Type 4  Step    Null    F1  140 137 406129
Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34
  • you can't just ask people to do the work for you. What did you try so far that didn't work for you? did you look for some solutions for this somewhere that you tried and didn't work? – Igor Meszaros Oct 06 '17 at 15:19
  • I see what your saying. Good point. I cant seem to post any sample coded here I I have been and searching for several days on this. – Jimmy Boone Oct 06 '17 at 15:31
  • This is a common google search and all the results I came back with didn't provide any solutions that seem d to fit my case. "read and sort a csv c#" – Jimmy Boone Oct 06 '17 at 15:33

1 Answers1

2

As said above - you can't really expect people to do the work for you... but I was bored.

Here is a simple solution in the form of a complete console app that will likely fall apart the second you give it real world data, but hopefully will get you started.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        //Read file
        var fileContents = File.ReadAllText("file.txt");

        //split on carriage returns and line feeds, remove empty entries.
        var lines = fileContents.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

        //Split each line on Tab
        var splitLines = lines.Select(l => l.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries));

        //splitLines is now an array of arrays.  Each splitLine entry is a line, and each entry of each splitline element is
        //a single field... so we should be able to sort how we want, e.g. by first field then by second field:
        var sortedLines = splitLines.OrderBy(sl => sl[0]).ThenBy(sl => sl[1]);

        //put back together as TSV - put tabs back.
        var linesWithTabsAgain = sortedLines.Select(sl => string.Join("\t", sl));

        //put carriage returns/linefeeds back
        var linesWithCRLF = string.Join("\r\n", linesWithTabsAgain);

        File.WriteAllText("newFile.txt",linesWithCRLF);


    }
}
}
GPW
  • 2,528
  • 1
  • 10
  • 22
  • :) You made my days. Thanks so much for posting. Works great. I had been researching this for almost a week and never found anything yet. – Jimmy Boone Oct 06 '17 at 15:40
  • I hope someone finds it as useful as I did too. – Jimmy Boone Oct 06 '17 at 15:41
  • You're welcome. Rather than sorting the data, instead turn it into something sortable, sort it then convert it back again. Obviously this approach wouldn't be great if the source data is huge as it loads the whole thing into memory... if you had memory problems it would probably be best to load it into a database for sorting stuff.. but it was an interesting little exercise for a Friday afternoon :) – GPW Oct 06 '17 at 15:46