Sean Probyn
Sean Probyn

Reputation: 123

How do i arrange the contents of a file in numerical order?

I've got these lines of code which reads a file and supposed to put the last digits on the line in number order:

string[] lines = File.ReadAllLines(@"g:\\myfile.DAT");
var result = lines.AsParallel()
    .OrderBy(s => s.Split('>').Last())
    .ToList();
result.ForEach(Console.WriteLine);

But its got decimal places in the answer. It works (sort of) but its putting 11 before 3.75. What have I done wrong?

Upvotes: 1

Views: 121

Answers (1)

Carlos Delgado
Carlos Delgado

Reputation: 336

By adding Convert.ToDouble into your OrderBy you can achieve your goal:

string[] lines = File.ReadAllLines(@"g:\\myfile.DAT");
var result = lines.AsParallel()
                  .OrderBy(s => Convert.ToDouble(s.Split('>').Last()))
                  .ToList();
result.ForEach(Console.WriteLine);

More info on Convert.ToDouble

Upvotes: 3

Related Questions