-1

I have an assignment where I have to sort a phone list by name. I have the following code. I know it's a mess and there are some things missing but it's what I got for now. Some variables are in Portuguese but I'll make my best to make it understandable. This is a different case where I am not allowed to use LINQ's or lists

static void Main(string[] args)
{
    string file= "nomes.txt";
    string[] text = File.ReadAllLines(file);

    // Reads line by line
    for (int i = 0; i < text.Length; i++)
    {
        string line = text[i];

        for (int j = 0; j < line.Length; j++)
        {
            if (char.IsWhiteSpace (line [j-1]) == true)
                {
                    string[] palavra = line.Split(',');

                    if (char.IsDigit(line [j]) == true)
                    {
                        string[] nr = line.Split('-');
                        Console.WriteLine(line);
                    }
                }
        }
    }

    Console.ReadKey();
}

File looks like this:

 Joaquim Lopes da Silva 932 000 999
 Maria da Conceição Granja 91 384 75 34
 Herculano Lopes Vieira 253334556

Expected output:

Silva, Joaquim Lopes da - 932 000 999
Granja, Maria da Conceição - 913 847 534
Vieira, Herculano Lopes - 253 334 556

1 Answers1

0

First read all data into a List< Item >:

class Item {
    public string First;
    public string Second;
    ...
}
var items = ReadFromFile();

Then use LINQ:

var sortedItems = items.OrderBy(entry, entry.First);

where entry.First is the string field for "Silva" or "Granja" etc.

David
  • 15,894
  • 22
  • 55
  • 66