C# Sorting, return multiple text file string entries line at a time -


i have c# console window program , trying sort "file3" (contains numbers) in ascending , output lines 3 text files. outcome looks this:

=========================================================================== field1.....................field2.....................field3 =========================================================================== [file1_line1]..............[file2_line1]..............[file3_line1] [file1_line2]..............[file2_line2]..............[file3_line2] [file1_line3]..............[file2_line3]..............[file3_line3] 

and on...

at moment, kinda works think duplicates first 2 lines seems. give example of better coding please? here code 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;  (double = 0.00; < file3_1.length; i++) {     (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) {     (int y = 0; y < file3_2.length; y++)     {         (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 more info, of code used program , worked in new program, doesn't i've said above - ("it repeats couple of lines reason"). i'm kinda amateur/ rookie @ c# stuff work examples.

thanks in advance :)

ok, if understand correctly, trying read lines 3 different files, each of them representing different "field" in table. want sort table based on value of 1 of field (in code, seems field values contained in file3. well, if got right, here's suggest do:

        // read data files         list<string> inputfilenames = new list<string> {"file1.txt", "file2.txt", "file3.txt"};         decimal[][] fieldvalues = new decimal[inputfilenames.count][];          (int = 0; < inputfilenames.count; i++)         {             string currentinputfilename = inputfilenames[i];             string[] currentinputfilelines = file.readalllines(currentinputfilename);             fieldvalues[i] = new decimal[currentinputfilelines.length];              (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));          (int = 0; < 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, tried keep code above linq free could, since not seem using in example, , therefore might not know it. being said, while there thousand way i/o in c#, linq lot in instance (and in pretty other situation really), suggest if don't know already.

also, datatable option proposed provide way visualize , organize data in more efficient way. being said, in no way obliged use datatable: stay more direct approach , use more common data structures (such lists, arrays or dictionaries if know are) store data, depending on needs. it's datatable, don't, example, need sorting yourself, or deal columns indexed integers. time, you'll come learn myriad of useful data structure , native functionalities c# language offers , how can save doing work in lot of cases.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -