Sort a Multidimensional array per column C #

3

I have a multidimensional array:

string[,] classificacao = new string[20, 2];

and it has the following inputs:

classificação[0,0]="Hamilton"; //ISTO É O NOME DO JOGADOR

classificação[0,1]="20"; //ISTO A SUA PONTUAÇÃO

classificação[1,0] = "Vettel";

classificação[1,1]="34";

Among others

How can I sort in descending order by the score column without losing the name of the player?

That is:

Vettel - 34

Hamilton - 20
    
asked by anonymous 31.05.2017 / 20:41

2 answers

6

The .NET Framework does not have functions to sort multidimensional arrays directly - sorting functions mostly work for jagged arrays. One solution is you convert your array to jagged , sort it, and then convert it back. The code below shows an example of this:

class PtStackOverflow_209128
{
    public static void Test()
    {
        string[,] classificacao = new string[4, 2];
        classificacao[0, 0] = "Hamilton"; //ISTO É O NOME DO JOGADOR
        classificacao[0, 1] = "104"; //ISTO A SUA PONTUAÇÃO
        classificacao[1, 0] = "Vettel";
        classificacao[1, 1] = "129";
        classificacao[2, 0] = "Bottas";
        classificacao[2, 1] = "75";
        classificacao[3, 0] = "Räikkönen";
        classificacao[3, 1] = "67";

        var jagged = ToJagged(classificacao);
        Array.Sort(jagged, (i1, i2) => int.Parse(i2[1]) - int.Parse(i1[1]));
        classificacao = ToRectangular(jagged);
        for (int i = 0; i < classificacao.GetLength(0); i++)
        {
            for (int j = 0; j < classificacao.GetLength(1); j++)
            {
                Console.Write("{0} ", classificacao[i, j]);
            }

            Console.WriteLine();
        }
    }

    static T[][] ToJagged<T>(T[,] array)
    {
        int height = array.GetLength(0), width = array.GetLength(1);
        T[][] jagged = new T[height][];

        for (int i = 0; i < height; i++)
        {
            T[] row = new T[width];
            for (int j = 0; j < width; j++)
            {
                row[j] = array[i, j];
            }
            jagged[i] = row;
        }
        return jagged;
    }
    static T[,] ToRectangular<T>(T[][] array)
    {
        int height = array.Length, width = array[0].Length;
        T[,] rect = new T[height, width];
        for (int i = 0; i < height; i++)
        {
            T[] row = array[i];
            for (int j = 0; j < width; j++)
            {
                rect[i, j] = row[j];
            }
        }
        return rect;
    }
}

(The conversion functions were copied this answer from the SOen).

    
31.05.2017 / 21:24
3

Arrays ??? Make a class my boy!

public class Classificacao
{
    public Classificacao()
    {

    }

    public Classificacao(String Piloto, Int32 Pontuacao)
    {
        this.Piloto = Piloto;
        this.Pontuacao = Pontuacao;
    }

    public String Piloto { get; set; }

    public Int32 Pontuacao { get; set; }
}

Then to Order, Lambda!

        List<Classificacao> Classif = new List<Classificacao>();
        Classif.Add(new Classificacao("Hamilton", 20));
        Classif.Add(new Classificacao("Vettel", 34));
        Classif.OrderBy(p => p.Pontuacao);

        foreach (Classificacao c in Classif)
        { 
            //faz alguma ação com seu List Ordenado
        }
    
31.05.2017 / 21:34