1

I have a C# console window program and I am trying to sort "File3" (contains numbers) in ascending and output lines from 3 text files. So the outcome looks something like this:

===========================================================================
field1.....................field2.....................field3
===========================================================================
[FILE1_LINE1]..............[FILE2_LINE1]..............[FILE3_LINE1]
[FILE1_LINE2]..............[FILE2_LINE2]..............[FILE3_LINE2]
[FILE1_LINE3]..............[FILE2_LINE3]..............[FILE3_LINE3]

and so on...

At the moment, it kinda works I think but it duplicates the first two lines it seems. Could someone give an example of better coding please? Here is the code that I have atm:

string[] File1 = System.IO.File.ReadAllLines(@"FILE1.txt");
string[] File2 = System.IO.File.ReadAllLines(@"FILE2.txt");
string[] File3 = System.IO.File.ReadAllLines(@"FILE3.txt");
decimal[] File3_1 = new decimal[File3.Length];

for(int i=0; i<File3.Length; i++)
{
    File3_1[i] = decimal.Parse(File3[i]);
}
decimal[] File3_2 = new decimal[File3.Length];

for(int i=0; i<File3.Length; i++)
{
    File3_2[i] = decimal.Parse(File3[i]);
}

decimal number = 0;

for (double i = 0.00; i < File3_1.Length; i++)
{
    for (int sort = 0; sort < File3_1.Length - 1; sort++)
    {
        if (File3_1[sort] > File3_1[sort + 1])
        {
            number = File3_1[sort + 1];
            File3_1[sort + 1] = File3_1[sort];
            File3_1[sort] = number;
        }
    }
}

if (SortChoice2 == 1)
{
    for (int y = 0; y < File3_2.Length; y++)
    {
        for (int s = 0; s < File3_2.Length; s++)
        {
            if (File3_1[y] == File3_2[s])
            {
                Console.WriteLine(File1[s] + File2[s] + File3_1[y]);
            }
        }
    }
}

Just for more info, most of this code was used for another program and worked but in my new program, this doesn't as I've said above - ("it repeats a couple of lines for some reason"). I'm kinda an amateur/ rookie at C# so I only get stuff like this to work with examples.

Thanks in advance :)

5
  • What do you sort those lines by? Commented May 31, 2015 at 16:29
  • 2
    Side note: for (double i = 0.00 - why do you want double for counter? Commented May 31, 2015 at 17:35
  • 1
    Example of better coding practice: of you are sorting anything - use existing methods like List<MyRecord>.Sort or Enumerable.OrderBy. Commented May 31, 2015 at 17:36
  • Excellent point, @AlexeiLevenkov! Best regards, Commented May 31, 2015 at 18:40
  • "why do you want double for counter?" I have no idea, I might have changed it by accident when changing other parts Commented Jun 1, 2015 at 15:56

1 Answer 1

1

Ok, if I understand correctly, what you are trying to do is read the lines from 3 different files, each of them representing a different "field" in a table. You then want to sort this table based on the value of one of the field (in you code, this seems to be the field which values are contained in File3. Well, if I got that right, here's what I suggest you do:

        // Read data from files
        List<string> inputFileNames = new List<string> {"File1.txt", "File2.txt", "File3.txt"};
        decimal[][] fieldValues = new decimal[inputFileNames.Count][];

        for (int i = 0; i < inputFileNames.Count; i++)
        {
            string currentInputfileName = inputFileNames[i];
            string[] currentInputFileLines = File.ReadAllLines(currentInputfileName);
            fieldValues[i] = new decimal[currentInputFileLines.Length];

            for (int j = 0; j < currentInputFileLines.Length; j++)
            {
                fieldValues[i][j] = decimal.Parse(currentInputFileLines[j]);
            }
        }

        // Create table
        DataTable table = new DataTable();

        DataColumn field1Column = table.Columns.Add("field1", typeof (decimal));
        DataColumn field2Column = table.Columns.Add("field2", typeof (decimal));
        DataColumn field3Column = table.Columns.Add("field3", typeof (decimal));

        for (int i = 0; i < fieldValues[0].Length; i++)
        {
            var newTableRow = table.NewRow();
            newTableRow[field1Column.ColumnName] = fieldValues[0][i];
            newTableRow[field2Column.ColumnName] = fieldValues[1][i];
            newTableRow[field3Column.ColumnName] = fieldValues[2][i];

            table.Rows.Add(newTableRow);
        }

        // Sorting 
        table.DefaultView.Sort = field1Column.ColumnName;

        // Output 
        foreach (DataRow row in table.DefaultView.ToTable().Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.Write(item + " ");
            }

            Console.WriteLine();
        }

Now, I tried to keep the code above as LINQ free as I could, since you do not seem to be using it in your example, and therefore might not know about it. That being said, while there is a thousand way to do I/O in C#, LINQ would help you a lot in this instance (and in pretty much any other situation really), so I suggest you look it up if you don't know about it already.

Also, the DataTable option I proposed is just to provide a way for you to visualize and organize the data in a more efficient way. That being said, you are in no way obliged to use a DataTable: you could stay with a more direct approach and use more common data structures (such as lists, arrays or even dictionaries if you know what they are) to store the data, depending on your needs. It's just that with a DataTable, you don't, for example, need to do the sorting yourself, or deal with columns indexed only by integers. With time, you'll come to learn about the myriad of useful data structure and native functionalities the C# language offers you and how they can save you doing the work yourself in a lot of cases.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the code and explanation etc. but it seems this code doesn't work in my program. I have added "using System.Data;" at the start but the program breaks on this line - "fieldValues[i][j] = double.Parse(currentInputFileLines[j]);"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.