title: Using insertion sort for descending order?
body: <p>I am trying to learn how to use Insertion Sort, and this is the main code I'm using:</p>
  
  <pre><code>for (j = 1; j &lt; num.length; j++)  // Start with 1 (not 0)
  {
      key = num[ j ];
      for(i = j - 1; (i &gt;= 0) &amp;&amp; (num[ i ] &lt; key); i--) // Smaller values are moving up
      {
          num[ i+1 ] = num[ i ];
      }
      num[ i+1 ] = key;  // Put the key in its proper location
   }
  </code></pre>
  
  <p>However, I've tried to change the - to + to try to change the output to descending order, but I'm confusing myself even more.</p>
  
  <p>This is the complete code I'm using:</p>
  
  <pre><code>public class InsertionSort {
  
      public static void main(String[] args) {
          int A[] = new int[10];
          populateArray(A);
          System.out.println("Before Sorting: ");
          printArray(A);
          // sort the array
          insertionSort(A);
          System.out.println("\nAfter Sorting: ");
          printArray(A);
      }
  
      /**
       * This method will sort the integer array using insertion sort algorithm
       */
      private static void insertionSort(int[] arr) {
          for (int i = 1; i &lt; arr.length; i++) {
              int valueToSort = arr[i];
              int j = i;
              while (j &gt; 0 &amp;&amp; arr[j - 1] &gt; valueToSort) {
                  arr[j] = arr[j - 1];
                  j--;
              }
              arr[j] = valueToSort;
          }
      }
  
      public static void printArray(int[] B) {
          System.out.println(Arrays.toString(B));
      }
  
      public static void populateArray(int[] B) {
          for (int i = 0; i &lt; B.length; i++) {
              B[i] = (int) (Math.random() * 100);
          }
      }
  }
  </code></pre>
  
  <p>Thanks for any help and suggestions</p>