Team table with 8 variables

1

I have a very difficult question, I have 8 teams in a table, I have already simulated the results but I need to update the table using the points, so I know the number of points of each team I wrote the following code:

        public void AtualizarTabela()
        {
        int TimePontos1 = Time1Vitoria * 3 + Time1Empate * 1;
        int TimePontos2 = Time2Vitoria * 3 + Time2Empate * 1;
        int TimePontos3 = Time3Vitoria * 3 + Time3Empate * 1;
        int TimePontos4 = Time4Vitoria * 3 + Time4Empate * 1;
        int TimePontos5 = Time5Vitoria * 3 + Time5Empate * 1;
        int TimePontos6 = Time6Vitoria * 3 + Time6Empate * 1;
        int TimePontos7 = Time7Vitoria * 3 + Time7Empate * 1;
        int TimePontos8 = Time8Vitoria * 3 + Time8Empate * 1;
        }

How can I make these TimesPoints variables in descending order so I can update the table? Given that I need to return each dot variable in a label later.

    
asked by anonymous 17.05.2017 / 04:26

1 answer

1

I would do the following:

public struct Estatistica
{
    public int Pontos { 
        get { return Vitorias * 3 + Empates; } 
    }

    public short Vitorias;
    public short Empates;
}

public void AtualizarTabela()
{
    Dictionary<string, Estatistica> times = new Dictionary<string, Estatistica>();
    time["Time 1"] = new Estatistica() { Vitorias = Time1Vitoria, Empates = Time1Empate };
    time["Time 2"] = new Estatistica() { Vitorias = Time2Vitoria, Empates = Time2Empate };
    time["Time 3"] = new Estatistica() { Vitorias = Time3Vitoria, Empates = Time3Empate };
    time["Time 4"] = new Estatistica() { Vitorias = Time4Vitoria, Empates = Time4Empate };
    time["Time 5"] = new Estatistica() { Vitorias = Time5Vitoria, Empates = Time5Empate };
    time["Time 6"] = new Estatistica() { Vitorias = Time6Vitoria, Empates = Time6Empate };
    time["Time 7"] = new Estatistica() { Vitorias = Time7Vitoria, Empates = Time7Empate };
    time["Time 8"] = new Estatistica() { Vitorias = Time8Vitoria, Empates = Time8Empate };
}

Sorting Descending:

    using System.Linq;

    foreach (var time in times.OrderByDescending(t => t.Value.Pontos)) 
    {
        // Escreva o que você precisa aqui. O nome do time está em 'time.Key'.
        // O resto das informações está em 'time.Value'.
        Console.WriteLine(time.Key);
    }

See an example here .

If it's Web Forms, you can do the following:

System.Web.UI.WebControls.Label[] labels = new System.Web.UI.WebControls.Label[] {
    lblTime1LugarPontos,
    lblTime2LugarPontos,
    lblTime3LugarPontos,
    lblTime4LugarPontos,
    lblTime5LugarPontos,
    lblTime6LugarPontos,
    lblTime7LugarPontos,
    lblTime8LugarPontos
};

Windows Forms:

System.Windows.Forms.Label[] labels = new System.Windows.Forms.Label[] {
    lblTime1LugarPontos,
    lblTime2LugarPontos,
    lblTime3LugarPontos,
    lblTime4LugarPontos,
    lblTime5LugarPontos,
    lblTime6LugarPontos,
    lblTime7LugarPontos,
    lblTime8LugarPontos
};

The loop then looks like this:

    foreach (var infoTime in times.OrderByDescending(t => t.Value.Pontos).Select((time, i) => new { Time = time, i = i })) 
    {
        // labels[infoTime.i].Text = infoTime.Time.Key;
        labels[infoTime.i].Text = infoTime.Time.Value.Pontos;
    }
    
17.05.2017 / 05:05