简体   繁体   中英

How to sort values in a two dimensional array suitable to the other values?

public class Spieler
{
    public Magier Henri = new Magier ("Magier Henri");    
    public Krieger Christian = new Krieger ("Krieger Christian");   
    public Supporter Judith = new Supporter ("Suppporter Judith");
    public Schuetze Lukas = new Schuetze ("Schuetze Lukas");
    public Magier Nick = new Magier ("Magier Nick");    
    public Krieger Anna = new Krieger ("Krieger Anna");   
    public Supporter Neele = new Supporter ("Suppporter Neele");
    public Schuetze Christoph = new Schuetze ("Schuetze Christoph");   
    public int i;
    public int besten5;
    public int bestPlayerEU;
    public int[][] kandidat = new int[8][2]; 
    int t;

    public void fuelleArray()
    {
        kandidat[0][0] = Henri.getGesamtPunkte();
        kandidat[0][1] = 1;
        kandidat[1][0] = Christian.getGesamtPunkte();
        kandidat[1][1] = 2;
        kandidat[2][0] = Judith.getGesamtPunkte();
        kandidat[2][1] = 3;
        kandidat[3][0] = Lukas.getGesamtPunkte();
        kandidat[3][1] = 4;
        kandidat[4][0] = Nick.getGesamtPunkte();
        kandidat[4][1] = 5;
        kandidat[5][0] = Anna.getGesamtPunkte();
        kandidat[5][1] = 6;
        kandidat[6][0] = Neele.getGesamtPunkte();
        kandidat[6][1] = 7;
        kandidat[7][0] = Christoph.getGesamtPunkte();
        kandidat[7][1] = 8;
    }

    public int[][] sort()
    {
        fuelleArray();
        int temp = 0;
        for (int i = 0; i < kandidat.length; i++)
        {
            for(int j = 1; j < kandidat.length-i; j++)
            {
                if(kandidat[j][t] < kandidat[j-1][t])
                {
                    temp = kandidat [j-1][t];
                    kandidat[j-1][t] = kandidat [j][t];
                    kandidat[j][t] = temp;
                }
            }
        } 
        return kandidat;
    }
}

So here I get the left side of my Array with random values sorted but I also want that the values on the right side of the two dimensional array move suitable to the characters. So I want that the kandidat "Henri" is still the value 1 , "Christian" = 2 , "Judith" = 3 , "Lukas" = 4 and so on. Can someone explain or write code so the numbers on the right side of the array move suitable to the characters / kandidat s?

So when the array looks like this in the beginning:

    0    1 
0   26   1
1   45   2
2   37   3
3   33   4
4   27   5
5   29   6
6   41   7
7   49   8

It should look like this in the end:

     0    1 
0   26    1
1   27    5
2   29    6
3   33    4
4   37    3
5   41    7
6   45    2
7   49    8

If I understand it correctly you want to swap the pairs of a "kandidate"+number keeping those pairs together. Have you tried something like this:

int[] temp = kandidat [j-1];
kandidat[j-1] = kandidat [j];
kandidat[j] = temp;

this swaps the pair (kanditate[j][0], kandidate[j][1]) with the pair (kanditate[j-1][0], kandidate[j-1][1]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM