title: Insertion Sort from Right to Left
body: <p>I am trying to learn Insertion Sort in Java but I have a problem. I learned the insertion sort in ascending order. Now, I need to do it in descending order but I have to start sorting the array from right to left.</p>
  
  <pre><code>public class Sorting {
  
  public static void sort(Comparable[] a) {
      int N = a.length;
      for (int i = 1; i &lt; N; i++) {                            
          for (int j = i; j &gt; 0 &amp;&amp; less(a[j], a[j - 1]); j--)
              swap(a, j, j - 1);
      }
  }
  
  private static void swap(Comparable[] a, int i, int j) { 
      Comparable t = a[i];
      a[i] = a[j];
      a[j] = t;
  }
  
  private static boolean less(Comparable v, Comparable w) {                                                   
      return v.compareTo(w) &lt; 0;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The code which is above sorts the array from left to right in ascending order(1,2,3,4,..).</p>
  
  <p>I need to change it in descending order but sorts from right to left(5,4,3,2..).
  I change the code and tried this:</p>
  
  <pre><code>    public static void sort(Comparable[] a) { 
      int N = a.length;
      for (int i = N-1; i&gt;=0; i--) { 
          for (int j = i; j &gt;= 1 &amp;&amp; less(a[j-1], a[j]); j++)
              swap(a, j, j-1);
      }
  }
  </code></pre>
  
  <p>I changed the sort method several times but I got errors..</p>