title: Java Collections.sort Comparator modifies private object ID?
body: <p>I'm aware this is a bit of a strange question. I have an ArrayList of constructed Continent objects. During construction, their private id is set, and only accessible through a getId() method. There is no setId() or the like for the Continent object. </p>
  
  <p>I am sorting this ArrayList using a custom Comparator from a Player object. </p>
  
  <pre><code>private ArrayList&lt;Continent&gt; sortContinentsByOwnership(ArrayList&lt;Continent&gt; continents)
  {
      Collections.sort(continents, new Comparator&lt;Continent&gt;(){
          @Override
          public int compare(Continent a, Continent b)
          {
              Float a_unowned_percentage = a.getUnownedPercentage();
              Float b_unowned_percentage = b.getUnownedPercentage();
              return a_unowned_percentage.compareTo(b_unowned_percentage);
          }
      });
      return continents;
  }
  </code></pre>
  
  <p>This works very well in ordering the objects. However, after using this method with the following code, the id field of the continents is 'munged' and set consistently with the other sorted objects. </p>
  
  <pre><code>contested_continents = this.sortContinentsByOwnership(contested_continents);
  </code></pre>
  
  <p>I have ensured that the objects are correctly sorted, but here is some example output accessing the continent's id property before and after the sort attempt. The id of the continent after the sort reflects the id field of the object that the sort was called from, in this instance a Player object.</p>
  
  <pre><code>Continents Pre-Sort - 0 2 6 7 8 
  Continents Post-Sort - 5 5 5 5 5 
  
  Continents Pre-Sort - 1 2 10 
  Continents Post-Sort - 0 0 0
  
  Continents Pre-Sort - 0 4 6 11 
  Continents Post-Sort - 1 1 1 1
  </code></pre>
  
  <p>Accessing other properties of these objects shows that it is still the same objects with the same elements outside of the id property. </p>
  
  <p>No where in the code, sort or otherwise, am I doing anything like continent.id = player.id, but the sort appears to be doing this somehow.</p>
  
  <p>Any suggestions or advice are welcome, and I am happy to provide additional source if necessary. Thanks kindly. </p>