Sort a Multidimensional array by column c#

Asked

Viewed 649 times

3

I have a multidimensional array:

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

and which 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 through the scoring column without losing the respective player’s name?

That is to say:

Vettel - 34

Hamilton - 20
  • You can start seeing the [tour]. Then you can go to [help]. If you still want to deepen go to [meta].

2 answers

6


. NET Framework does not have functions to sort multidimensional arrays directly - sorting functions work mainly for arrays Jagged. One solution is you convert your array to Jagged, order 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 have been copied of this answer soen).

  • so I’m seeing this code not to incorporate in a button; is that I’m working on Windows Form

  • @Tiagopereira gives yes to incorporate in a button or any other action that is pertinent the use of this code.

  • is already a start, but I already copied this code to my button and there are methods that n are recognized

  • @Carlosfigueira I was able to adapt the code to my program and it worked beautifully, although n understands nothing of the code, obg in the same

3

Arrays ??? Makes 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 Sort, 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
        }
  • actually in my program I have an array with 20 players, I randomly sort the array, to know the 1 sorted and so on, and consonant

  • Change the line Classif.Orderby(p => p.Score); to Classif.Orderbydescending(p => p.Score); So it will sort downwards, by score

  • I agree that a class is a better solution, but the question was not for that solution, excuses, but, I find it unfair.

  • Unjust in what sense ?

  • 1

    Your reply @Juliosoares answers perhaps another question is an excellent answer, but not what the question asked. One could even put two solutions and this one being the best, but it should then contain an answer on array multidimensional ... There is the fault is not yours only was an indication.

  • 1

    I agree with @Virgilionovic. This answer solves the main problem (sort the data), but it does not do so as requested.

  • Really @Virgilionovic, the answer does not resolve the question as requested but suggests to anyone who has a similar question the possibility to decide whether to keep the array or part for a new approach. Sometimes, it can give a light !.. But I will focus more on the questions in the next answers... Hugs !

Show 2 more comments

Browser other questions tagged

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