2

I have a file with some characteristics in particular, but, in this file, in column 30, I have a number, not sequential, 5 digit, and I would like to arrange the lines of this file, based on this numeroação, in ascending order.

For example:

sample sample sample1        00094
sample sample sample2        00001
sample sample sample3        00032
sample sample sample4        00491
sample sample sample5        00002
sample sample sample6        00010
sample sample sample7        00007

The sequential number is the 00094, 00001...

And the output is:

sample sample sample2        00001
sample sample sample5        00002
sample sample sample7        00007
sample sample sample6        00010
sample sample sample3        00032
sample sample sample1        00094
sample sample sample4        00491

I developed a sitema extremely pig, where I play everything in a list, and make a bubble sort, then, I wonder if there is a more efficient way of doing this, perhaps using Linq, or even a different way.

Thanks for the help.

Alexandre
  • 1,985
  • 5
  • 30
  • 55

2 Answers2

4
string[] lines = File.ReadAllLines(@"C:\temp\yourFile.txt");

var sotedLines = lines.Skip(1)
                      .Take(lines.Length - 2)
                      .OrderBy(getKeyFromLine)
                      .ToArray();

sortedLines.ForEach(Console.WriteLine);

Now if your columns are separated by space, use next getKeyFromLine implementation:

Func<string, int> getKeyFromLine = line => int.Parse(line.Split(' ')[30]);

If it's simply the 30 - 35 one-based indexing digits in a line, then use

Func<string, int> getKeyFromLine = line => int.Parse(line.Substring(29, 5));
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • it's possible to make to not process the first and the last line of the file ? – Alexandre Jun 11 '13 at 14:53
  • If this were a big file and you didn't want to read it into memory, you could try something like `File.ReadLines(@"C:\temp\yourFile.txt") .Skip(1).Reverse().Skip(1).Reverse().OrderBy(getKeyFromLine)` to keep the contents in an enumerable. – neontapir Jun 11 '13 at 15:02
  • Evaluation of Reverse is deferred until you actually try to pull the data. In combination with File, though, you might be right. Most testing is needed. – neontapir Jun 11 '13 at 17:08
  • It's deferred, but not streaming. When you read the first item - you will read all of them and `File.ReadLines` helps a little in this scenario. – Ilya Ivanov Jun 11 '13 at 17:11
1

How about:

string[] lines = File.ReadAllLines(@"text.txt");
var result = lines.AsParallel()
    .OrderBy(s => s.Split(' ').Last())
    .ToList();
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • It's not space, that separates columns. By `column` OP means character position in a line. He needs 30-35 characters. It took some time to figure it out for me (see comments) – Ilya Ivanov Jun 11 '13 at 14:50
  • But the digits are still the last non-space chars on each line? – Alex Filipovici Jun 11 '13 at 15:01
  • I don't know. It could be the case, that they aren't in the end of the line. Moreover, you need to order then alphanumerically, but not aplhabetically, so you probably also need to use `int.Parse` – Ilya Ivanov Jun 11 '13 at 15:41