# Sorting 2D Array problem

• May 4th, 2013, 10:44 AM
lordofrandom
Sorting 2D Array problem
Hi, I am using a bubble sort algorithm to sort a 2D array. The array is as follows:

Code :

```String data[][] = {{"John", "Smith", "14.75", "30", "ZZZZ", "ZZZZ", "ZZZZ",},{"Bill", "Gates", "18.50", "40","ZZZZ", "ZZZZ", "ZZZZ",}, {"Steve","Jobs", "10.75", "25","ZZZZ", "ZZZZ", "ZZZZ",},{"Paul", "Allen","21.50","34","ZZZZ", "ZZZZ", "ZZZZ",}, {"Steve", "Ballmer", "18.75", "35","ZZZZ", "ZZZZ", "ZZZZ",},{"ZZZZ", "ZZZZ", "ZZZZ", "ZZZZ"}}; //ZZZZ are marker values,the last row is to hold total values of all employees```

I want to sort this 2D array by comparing last names (index 1), and bubble sorting all their corresponding values along with it.

Code :

```static public String[][] sort(String data[][]) { for(int x = 0; x <= data.length-1; x++) { for(int y = 0; y<=data.length-2;y++) { if(data[y][1].compareTo(data[y+1][1]) > 0) //compare lastnames alphabetically { String temp = data[y][1]; //swap last names data[y][1]=data[y+1][1]; data[y+1][1] = temp;   String temp2 = data[y][0]; //swap first names data[y][0]=data[y+1][0]; data[y+1][0] = temp2;   String temp3 = data[y][2]; //etc data[y][2]=data[y+1][2]; data[y+1][2] = temp3;   String temp4 = data[y][3]; data[y][3]=data[y+1][3]; data[y+1][3] = temp4;   String temp5 = data[y][4]; data[y][4]=data[y+1][4]; data[y+1][4] = temp5;   String temp6 = data[y][5]; data[y][5]=data[y+1][5]; data[y+1][5] = temp6;   } }     }```

It compiles but when I run it i get the error:
java.lang.ArrayIndexOutOfBoundsException: 4
• May 4th, 2013, 10:49 AM
Norm
Re: Sorting 2D Array problem
Quote:

.ArrayIndexOutOfBoundsException
Array indexes range from 0 the the array length-1
Check the code to see which index is going past the end of the array.
If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

If you had copied the full text of the error message it would show the line number of the error and the value of the index.

Hint: the second dim can be handled as a single dim array
Code :

```int[][] twoDim = {{1,2,3}, {4,5,6}}; // swap the one dim array parts int[] oneDimTemp = twoDim[0]; // save twoDim[0] = twoDim[1]; // move twoDim[1] = oneDimTemp; System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]```
• May 4th, 2013, 11:25 AM
lordofrandom
Re: Sorting 2D Array problem
Quote:

Originally Posted by Norm
Array indexes range from 0 the the array length-1
Check the code to see which index is going past the end of the array.
If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

If you had copied the full text of the error message it would show the line number of the error and the value of the index.

Hint: the second dim can be handled as a single dim array
Code :

```int[][] twoDim = {{1,2,3}, {4,5,6}}; // swap the one dim array parts int[] oneDimTemp = twoDim[0]; // save twoDim[0] = twoDim[1]; // move twoDim[1] = oneDimTemp; System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]```

Thanks a lot, sir.