Make Sort of a list

Asked

Viewed 62 times

0

I am trying to make a Sort of a list that has information from a text file, to fetch the information from the file I use:

string ficheiro = "tvInfo.txt";
List<string> Classificacoes;
var linha = File.ReadAllLines(ficheiro);
Classificacoes = new List<string>();

foreach (var linhas in linha)
{
    Classificacoes.Add(linhas);
}

After fetching the information the list looks like this:

Liverpool;50
Arsenal;10
Benfica;20
PSG;30

I wanted to do the list Sort from the second field. I’m trying to use Linq but I can’t get past here:

Classificacoes.OrderByDescending(c => c);
  • It’s pretty simple, you made some code?

  • 1

    The code I have is just to fill the list with the information from the string file string file = "tvInfo.txt"; List<string> Classifications; var line = File.Readalllines(file); Classifications = new List<string>(); foreach (var lines in line) { Classifications.Add(lines); }

  • So this list just use Linq with the Orderby method that solves

  • That’s what I’ve been trying to do Classificacoes.Orderbydescending(c=>c), but the problem is that I can’t complete the code

  • You need to put this in your question and better organize your code that text still needs to be broken in two parts each row

1 answer

2

Come on.

First you need to sort the list from the name or the score?

If it’s from the name, it’s quiet. Just use the Linq Thus:

Classificacoes = Classificacoes.OrderByDescending(c => c).ToList();

That’s because the OrderByDescending return an object to you, so it’s no use you putting it in the void there.


Now, if you need to sort by number, I recommend using a dicionario

Dictionary<string, int>()

where your team’s name is the key, and the number is the score, so you can use the Linq equally, only now, ordering by value:

Classificacoes = Classificacoes.OrderByDescending(key => key.Value).ToList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.