# Bubble Sort For 2D Arrays

• 11-30-2011, 10:20 PM
tmantonym
Bubble Sort For 2D Arrays
Hi everyone, I'm new to java programming, and was trying to make some sort of leaderboard system. I'm having a problem sorting the elements by their 2nd dimensions. I'm trying to use a bubble sort algorithm, but for some reason all players end up ranked as no.1 after going through my attempt at a bubble sort. There are 1000 players.

Can anyone help? This is just to let you know how I'm trying to sort things:
• [][3] is the player score. (What change is based on)
• [][4] is the player rank. (What I want to change)

Code:

```        boolean flag = true;             String temp;             while (flag)             {                     flag = false;                 for (int j = 0; j < Player.player.length - 1; j++)                 {                         System.out.println(""+Player.player[j][3]+" "+Player.player[j+1][3]);                         double currentScore = Double.parseDouble(Player.player[j][3]);                         double nextScore = Double.parseDouble(Player.player[j+1][3]);                         if (currentScore < nextScore && currentScore != 0)    // '>' for ascending sort                     {                               temp = Player.player[j][4];                         Player.player[j][4] = Player.player[j+1][4];                         Player.player[j+1][4] = temp;                         flag = true;                         System.out.println("A swap occured.");                     }                         else                         {                                 System.out.println("A swap didn't occur.");                         }                         System.out.println(""+ Player.player.length);                         System.out.println(""+Integer.parseInt(Player.player[j][3])+" "+ Integer.parseInt(Player.player[j+1][3]));                 }                 System.out.println("New algorithm finished");             }```
• 11-30-2011, 10:33 PM
Norm
Re: Bubble Sort For 2D Arrays
Can you post the output from the program that shows what it does and add some comments to explain what is wrong with the output. Write a small test program with a 2 dim array for input to your method. Print out the before and after contents of the array.
You can use the Arrays toString method to print out the second dimension. Print the first yourself.
• 11-30-2011, 10:40 PM
tmantonym
Re: Bubble Sort For 2D Arrays
Okay, I won't be able to do this tonight (it's 20:40 here in England) so I'll provide all this tomorrow. Thanks for the fast reply!