title: Java - Sorting a 2D Array by Row Sum
body: <p>Trying to write a method that swaps the rows of a 2D array in order of increasing row sum.</p>
  
  <p>For example, if I have the following 2d array:</p>
  
  <pre><code>int [][] array = {4,5,6},{3,4,5},{2,3,4};
  </code></pre>
  
  <p>I would want it to output an array as so:</p>
  
  <pre><code>{2 3 4}, {3 4 5}, {4 5 6}
  </code></pre>
  
  <p><em>Methodology:</em> </p>
  
  <p>a.) take the sums of each row and make a 1D array of the sums</p>
  
  <p>b.) do a bubble sort on rowSum array </p>
  
  <p><strong>c.) swap the rows of the original array based on the bubble sort swaps made</strong></p>
  
  <p>d.) then print the newly row sorted array.</p>
  
  <p>Here's my code so far: </p>
  
  <pre><code>      public void sortedArrayByRowTot() {
          int [][] tempArray2 = new int [salaryArray.length [salaryArray[0].length];
  
          for (int i = 0; i &lt; tempArray2.length; i++) {
            for (int j = 0; j &lt; tempArray2[i].length; j++) {
              tempArray2[i][j] = salaryArray[i][j];
            }
          }
  
          int [] rowSums = new int [tempArray2.length];
          int sum = 0;
          for (int i = 0; i &lt; tempArray2.length; i++) {
            for (int j = 0; j &lt; tempArray2[i].length; j++) {
              sum += tempArray2[i][j];
            }
            rowSums[i] = sum;
            sum = 0;
          }
  
          int temp;
          int i = -1;
          for(int j = rowSums.length; j &gt; 0; j--){
            boolean isSwap = false;
            for (i = 1; i &lt; j; i++) {
              if(rowSums[i-1] &gt; rowSums[i]) {
                temp = rowSums[i-1];
                rowSums[i-1] = rowSums[i];
                rowSums[i] = temp;
                isSwap = true;
              }
            }
  
            if(!isSwap){
              break;
            }
          }
  
          for (int k = 0; k &lt; tempArray2.length; k++) {
            temp = tempArray2[i-1][k];
            tempArray2[i-1][k] = tempArray2[i][k];
            tempArray2[i][k] = temp;
          }
  
          for (int b = 0; b &lt; tempArray2.length; b++) {
            for (int c = 0; c &lt; tempArray2[b].length; c++) {
              System.out.print(tempArray2[b][c] + " ");
            }
          }
        }
      }
  </code></pre>
  
  <p>Not sure if I am doing part c of my methodology correctly? </p>
  
  <p>It keeps saying <strong>"Exception in thread "main"</strong> java.lang.ArrayIndexOutOfBoundsException: 2"</p>