title: Sorting an array using two different criteria
body: <p>I am trying to sort an array using the insertion sort algorithm. The array is filled with <code>WordNode</code> elements that include a <code>word</code> field (inputted from a text file) and a <code>frequency</code> field (to measure the number of times the particular word appears in the text file). I have implemented the sort so that words are sorted by frequency (from lowest to highest), but I also want to sort alphabetically if frequencies are equal. How can I sort using two different criteria at the same time? Below is my sort code.</p>
  
  <pre><code>public static void sort(ArrayUnorderedList&lt;WordNode&gt; array) {
      //create stacks for insertion sort
      LinkedStack&lt;WordNode&gt; sorted = new LinkedStack&lt;WordNode&gt;();
      LinkedStack&lt;WordNode&gt; temp = new LinkedStack&lt;WordNode&gt;();
  
      //while the array has elements to be sorted
      while(!array.isEmpty()) {
          //remove current element from array
          WordNode currentNode = array.removeFirst();
  
          //while the sorted stack meets sorting criteria
          while((!sorted.isEmpty()) &amp;&amp; (sorted.peek().getFrequency() &lt; currentNode.getFrequency())) {
              //push elements to temp stack
              temp.push(sorted.pop());
          }
  
          //push current element to sorted stack
          sorted.push(currentNode);
  
          //while the temp stack has elements to be replaced
          while(!temp.isEmpty()) {
              //push elements to sorted stack
              sorted.push(temp.pop());
          }
      }
  
      //replace sorted elements in array
      while(!sorted.isEmpty()) {
          array.addToRear(sorted.pop());
      }
  }
  </code></pre>