title: How to sort a two dimension array (each row) in Java
body: <h2> To solve a problem i need to sort each row of a two dimension array.</h2>
  
  <p>Initial array</p>
  
  <pre><code>35 22 42 28 20 11
  35 21 14 33 1  34
  35 42 16 22 38  8
  29 12 42 25 28  2
  25 28 22 11 20 35
  1  40 41 43 44 45
  </code></pre>
  
  <p>After Sort:</p>
  
  <pre><code>11 20 22 28 35 42 
   1 14 21 33 34 35
   8 16 22 35 38 42
   2 12 25 28 29 42
  11 20 22 25 28 35
   1 40 41 43 44 45
  </code></pre>
  
  <p><strong>Followed this docs</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/7937134/sorting-a-2d-integer-array-based-on-a-column">Sorting a 2D Integer array based on a column</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array">java Arrays.sort 2d array</a>  </p>
  
  <p>all of them suggested to <code>use Comparator.</code>   </p>
  
  <p><code>But i got an impression that it compares its two arguments. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.</code></p>
  
  <p><code>So couldn't really make it work for my case (6 columns).</code></p>
  
  <p><strong>Any idea/pointer would be helpful.</strong></p>
  
  <p><strong>After taking the Input this is what i've tried</strong></p>
  
  <pre><code>    Arrays.sort(arr, new Comparator&lt;int[]&gt;() {
          @Override
          public int compare(int[] int1, int[] int2) {
              return Integer.valueOf(int1[0]).compareTo(int2[0]);
          }
      });
      for(c=0;c&lt;6;c++) {
          for(d=0;d&lt;6;d++) {
              System.out.println(arr[c][d]);
          }
          System.out.println();
      }        
  </code></pre>
  
  <p><strong>and getting this..</strong></p>
  
  <pre><code>35 22 42 28 20 11
  35 21 14 33 1 34
  35 42 16 22 38 8
  29 12 42 25 28 2
  25 28 22 11 20 35
  1 40 41 43 44 45
  
  1
  40
  41
  43
  44
  45
  
  25
  28
  22
  11
  20
  35
  
  29
  12
  42
  25
  28
  2
  
  35
  22
  42
  28
  20
  11
  
  35
  21
  14
  33
  1
  34
  
  35
  42
  16
  22
  38
  8
  </code></pre>