title: Java Insertion Sort with an Array List of objects?
body: <p>I have a class Card which contains value (int), suit (String) and faceValue (String). It seems like a regular insertion sort on Card.value should work fine. I just use the whole object when moving things around. For some reason, this crashes and burns. It ends up duplicating the highest card into every element except for a random element that I can't understand.</p>
  
  <p>value, suit, and faceValue are pulic, also.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public static void insertionSort(ArrayList&lt;Card&gt; Array) {
  
      int i,j;
      Card key = new Card(0, "","");
  
      for (i = 1; i &lt; Array.size(); i++) {
          key.value = Array.get(i).value;
          key.suit = Array.get(i).suit;
          key.faceValue = Array.get(i).faceValue;
          j = i;
          while((j &gt; 0) &amp;&amp; (Array.get(j - 1).value &gt; key.value)) {
              Array.set(j,Array.get(j - 1));
              j--;
          }
          Array.set(j,key);
      }
  
  
  }
  </code></pre>
  
  <p>I checked this against Wikipedia's pseudo code, and I can't find any fundamental difference. I've been through the debugger a dozen times, and I can't see any reason for the compiler to do what it's doing. Does anyone have an idea why it's not working?</p>
  
  <p>Thanks.</p>