1

I am attempting to sort xls lines by fourth string in lines.

string[] list_lines = System.IO.File.ReadAllLines(@"E:\VS\WriteLines.xls");



        // Display the file contents by using a foreach loop.
        System.Console.WriteLine("Contents of Your Database = ");

        foreach (var line in list_lines)
        {

            // Use a tab to indent each line of the file.
            Console.WriteLine("\t" + line);
        }

I am having problems creating algorithm that will identify the fourth element of each line and list content in alphabetical order. The words in each line are separated by ' '.

Can anyone put me on a right direction please?

EDIT-------------------------- ok,

foreach (var line in list_lines.OrderBy(line => line.Split(' ')[3]))

sorted the problem. Lines are sorted as I need. Excel changes ' ' spaces with ';'. That's why when compiled it was giving error.

Now, I guess, I need to parse each part of string to int since it sorts by first digit and not by a number.

5
  • What do you want to do when a string doesn't have a 4th item, as found in @Patrick Hofman 's answer? Commented Apr 13, 2016 at 10:44
  • it's strange. I am sure that xls file is populated in 5 columns Commented Apr 13, 2016 at 10:46
  • They are probably being separated by another symbol than space. Have you check the lines while debugging? Commented Apr 13, 2016 at 10:47
  • Draken, You were right. It seems that excel puts ';' on place of empty spaces. Commented Apr 13, 2016 at 10:51
  • The lines get sorted by any column I need. Thank you. Commented Apr 13, 2016 at 10:51

2 Answers 2

1

You can split the lines and then use the third item in an OrderBy:

foreach (var line in list_lines.OrderBy(line => line.Split(' ')[3]))
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your advice. Your code doesn't cause error, but when compiled I get strange error and code doesn't work.
Then not every line has a fourth item.
they do, there are 5 columns in the file. all populated by numbers.
1

Well, just sort the array:

string[] list_lines = ...;

// General case: not all strings have 4 parts
Array.Sort(list_lines, (left, right) => {
  String[] partsLeft = left.Split(' ');
  String[] partsRight = right.Split(' ');

  if (partsLeft.Length < 4)
    if (partsRight.Length < 4)
      return String.Compare(left, right, StringComparison.OrdinalIgnoreCase) 
    else 
      return -1;
  else if (partsRight.Length < 4)
    return 1; 

  return String.Compare(partsLeft[3], partsRight[3], StringComparison.OrdinalIgnoreCase);
});

If all the lines guaranteed to have 4 items at least it can be simplfied into

Array.Sort(list_lines, (left, right) => 
  String.Compare(left.Split(' ')[3],
                 right.Split(' ')[3],
                 StringComparison.OrdinalIgnoreCase));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.