Click here to Skip to main content
15,885,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a txt file containing related values such as these:

ColumnOne, ColoumnTwo
1.1, 0.1836856
3.3, -5.5
-0.38162, 6.636666E-08
1.01516E-07, 0.3695395

I would like to order these values using the second column then write them to a new txt file.
How can I do this without modifying any values ?
i.e. if the original file contains 6.636666E-08, then the new txt file needs to contain that value and not a converted value such as: 0.00000006636666

Thank you.

[Update]
How can read the file into a Tuple ?
Thank you
C#
var reader = new StreamReader(File.OpenRead(@"C:\MyFile.txt"));

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                var val1 = decimal.Parse(values[0], System.Globalization.NumberStyles.Float);
                var val2 = decimal.Parse(values[1], System.Globalization.NumberStyles.Float);
                var val3 = values[2];

                Tuple<decimal,> myTuple = Tuple.Create<decimal,decimal,string>(val1,val2, val3);

            }</decimal,decimal,string></decimal,>
Posted
Updated 17-Oct-15 9:21am
v3
Comments
PIEBALDconsult 17-Oct-15 12:08pm    
Use tuples or something. Have one contain the "raw" string value and the other contain the numeric value. Sort by the numeric. Write the string.
walidm38 17-Oct-15 14:59pm    
If my txt file contained similar rows to this:
1.1, 0.1836856, 5.6
3.3, -5.5, 43.44
-0.38162, 6.636666E-08, 3.1
1.01516E-07, 0.3695395, 1.5

How can read the file into a Tuple ?
Thank you

Hide Copy Code
var reader = new StreamReader(File.OpenRead(@"C:\MyFile.txt"));

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');

var val1 = decimal.Parse(values[0], System.Globalization.NumberStyles.Float);
var val2 = decimal.Parse(values[1], System.Globalization.NumberStyles.Float);
var val3 = values[2];

Tuple<decimal,> myTuple = Tuple.Create<decimal,decimal,string>(val1,val2, val3);

}
Patrice T 17-Oct-15 15:22pm    
I have used the improve question for you.

You may employ LINQ to objects, e.g.
C#
string filein = @"datain.txt";
var sorted = from line in File.ReadAllLines(filein)
             let record = Tuple.Create<double, string>(double.Parse(line.Split(',')[1]), line)
             orderby record.Item1
             select record.Item2;
string fileout = @"dataout.txt";
File.WriteAllLines(fileout, sorted.ToArray());
Cheers
Andi
 
Share this answer
 
Comments
[no name] 18-Oct-15 12:51pm    
I like it so a +5. I'm not sure whether it is something Overkill for OP.
Andreas Gieriet 18-Oct-15 13:45pm    
Thanks for your 5.
I was reluctant to post it. But, hey, it's showing some approaches to read a file line-wise, create a aux structure to sort/filter items and return the items again.
If it's a homework assignment, the OP has to chew something ;-) If it's for production code, I think it's worthwhile to consider such a solution.
Cheers
Andi
Here is a simplified version of your code:
C#
var reader = new StreamReader(File.OpenRead(@"C:\MyFile.txt"));
 
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
 
                var val1 = decimal.Parse(values[1], System.Globalization.NumberStyles.Float);
                
 
                Tuple<decimal> myTuple = Tuple.Create<decimal,string>(val1,line);
 
            }
            // sort here on the decimal value
            
            // write here result to text file

</decimal>

I think myTuple should be a list
Nota: I am not a specialist of C#.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900