output text file according the column values

zhshqzyc
zhshqzyc used Ask the Experts™
on
Hi, I have a text file.
I want to order the values from small to large by column n, then output to a new file. Hopefully linq code.

Thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
By the way, from the second row.
The first line is a header which is not numeric.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Can you provide a sample, real or replicated, of what the data looks like?

Author

Commented:
P_HWD	P	BP	NMISS	BETA	SE	
4026	38/492/1483	0.7826	0.03842	125456933	1993	
2589      135/770/1068	0.8619	0.6415	21227772	1952
4016	1/105/1902	1	0.7665	1347325	        1987
1234      424/1024/568	0.3705	0.2921	185118462	1995	

Open in new window

For example order by column 5.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I want to order the values from small to large
By text length, or actual (converted) value?

Author

Commented:
By actual (converted) value.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Your sample data appears to be tab-delimited. If so, then the following should be adequate; if not, then we can modify the split operation accordingly.
string[] lines;
string heading;

using (System.IO.StreamReader reader = new System.IO.StreamReader("input.txt"))
{
    heading = reader.ReadLine();

    var sorted = from line in reader.ReadToEnd().Split('\n')
                 let columns = line.Split('\t')
                 orderby Convert.ToDouble(columns[4])
                 select line;

    lines = sorted.ToArray();
}

using (System.IO.StreamWriter writer = new System.IO.StreamWriter("output.txt"))
{
    writer.WriteLine(heading);
    writer.Write(string.Join("\n", lines));
}

Open in new window

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

As you can see, there is no error checking, so you'll probably want to add some (especially with "Convert.ToDouble()").

Author

Commented:
If we restrict the value of column 3 within a scope. Can we say
using (System.IO.StreamReader reader = new System.IO.StreamReader("input.txt"))
{
    heading = reader.ReadLine();

    var sorted = from line in reader.ReadToEnd().Split('\n')
                 let columns = line.Split('\t')
                 where(column[3]>1000.00 && column[3]<=5000.00)
                 orderby Convert.ToDouble(columns[4])
                 select line;

    lines = sorted.ToArray();
}

Open in new window

Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Well you'll need to again do a conversion, but sure. You'll only get the lines that satisfy the where condition, obviously.
var sorted = from line in reader.ReadToEnd().Split('\n')
             let columns = line.Split('\t')
             let converted = Convert.ToDouble(columns[3])
             where (converted > 1000.00D && converted <= 5000.00D)
             orderby Convert.ToDouble(columns[4])
             select line;

Open in new window

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial