Q:

Implement the following method to sort the rows in a twodimensional array

I have been working on this problem all day and cannot find out what to do next. I have it sorting the rows but it will not sort the last row completely. Here is my code.I know once I or someone gets this I will feel stupid .Thank you
    public class Sort2DRow
{
   public static void main (String [] args)
   {
      int[][] matrix = {{3,5,6}, {4,1,2},{9,8,7}};

      System.out.println("Before the sort");
      for(int row = 0; row <matrix.length; row++){
         for(int col = 0; col <matrix[row].length; col++){
            System.out.print(matrix[row][col] + " ");
         }
         System.out.println();
      }

      System.out.println();//Spacer
      System.out.println("After sort method");
      sortRow(matrix);

   }

   public static int[][] sortRow(int[][] m)
   {
      int temp = 0;
      for(int row = 0; row < m.length ; row++)
      {
         for(int col = 0; col < m.length -1; col++){
            if(m[row][col] > m[row][col + 1])
            {
               temp = m[row][col];
               m[row][col] = m[row][col + 1];
               m[row][col + 1] = temp;
            }
         }
       }

       for(int row = 0; row <m.length; row++){
         for(int col = 0; col <m[row].length; col++){
            System.out.print(m[row][col] + " ");
         }
         System.out.println();
      }

       int[][] result = m;
       return result;
   }

}

A:

You're on the right track, but the problem is that you're mistakenly assuming that for(int row = 0; row < m.length ; row++) is your outer bubblesort loop. It's actually just the loop that "loads" the next row in the 2-d array. So you'll need to add the missing loop:
for(int col = 0; col < m[row].length; col++){ //This is the real outer bubblesort loop. Change it to m[row].length
    for(int nextCol = col; nextCol < m[row].length; nextCol++) {
        if(m[row][col] > m[row][nextCol])
        {
           temp = //I'll let you figure this out
           m[row][col] = //....
           m[row][nextCol] = //...
        }
     }
}

