Im making a scoreboard for a game that im creating but I need to make a scoreboard which will sort the scores in decesding order using a bubble sort algorithim. However, the corresponding names of the sorted score list would be muddled up. I have searched for an answer but they are done with array.sort which isnt my own sorting algorithim. Please help me sort the scores while the correct names will be printed along side them.
for now lets say:
int[] scores={6,3,15,12};
String[] names={"player1","player2","player3","player4"};
public static void results(String [] names, int [] results) {
int temp;
boolean sorted=false;
while(sorted==false) {
sorted = true;
for(int i=0; i<results.length-1; i++) {
if(results[i] < results[i+1]) {
temp = results[i];
results[i]=results[i+1];
results[i+1] = temp;
sorted = false;}
}
}
for(int i=0; i<results.length; i++) {
print("In position " + (i+1) + " is " + names[i] + " with " + results[i] + " points.");}
}
public static void resultscalc(int [] score, int [] results) {
for(int i=0; i<score.length; i++) {
results[i] = score[i]; }
}
If you really have to do it like that you could just add another temp variable and swap the items in names at the same time you swap the results. ie. just duplicate the first 3 lines in the if statement.
But it would be better to make a Player class that has a name and a result, then just sort an array of players. Would look something like this
if(players[i].getResult() < players[i+1].getResult()) {
temp = players[i];
players[i]=players[i+1];
players[i+1] = temp;
sorted = false;}
}
and then just players[i].getName() for the print statements.