title: Java Collections.sort() not comparing elements
body: <p>I have an ArrayList of IndexEntry's which is a simple class with a string, and two integers and a <code>compareTo()</code> method shown below.  My issue is that when I call <code>Collections.sort()</code> on the ArrayList most of the time everything sorts correctly, but sometimes two elements will never be compared resulting in an incorrectly sorted list.</p>
  
  <p>Some code for reference:</p>
  
  <pre><code>//Get a list of the values
  ArrayList&lt;IndexEntry&lt;T&gt;&gt; cleanEntries = new ArrayList&lt;IndexEntry&lt;T&gt;&gt;(matches.get(query).values());
  
  //Sort Files/URLS
  Collections.sort(cleanEntries);
  
  public int compareTo(IndexEntry&lt;T&gt; other) {
      if(this.occurrenceCount &lt; other.getOccurrenceCount()){
          return 1;
      }
      else if(this.occurrenceCount &gt; other.getOccurrenceCount()){
          return -1;
      }
      else{//Need to compare Initial Positions
          if(this.firstOccurrence == other.getFirstOccurrence()){
              return this.getLocationName().compareTo(other.getLocationName());
          }
          else if(this.firstOccurrence &gt; other.getFirstOccurrence()){
              return 1;
          }
          else{
              return -1;
          }
      }
  }   
  </code></pre>
  
  <p>Here is some example output for all IndexEntry's for the letter "C":</p>
  
  <pre><code>c
  "index.html", 6, 2
  "chickadee.html", 3, 2
  "cuckoo.html", 3, 2
  "crow.html", 3, 2
  "crane.html", 3, 2
  "cardinal.html", 3, 2
  "quail.html", 2, 2
  "robin.html", 2, 2
  "finch.html", 2, 2
  "kingfisher.html", 2, 2
  "swan.html", 2, 2
  "turkey.html", 2, 2
  "ibis.html", 2, 2
  "falcon.html", 2, 2
  "albatross.html", 2, 2
  "tern.html", 2, 2
  "dove.html", 2, 2
  "mockingbird.html", 2, 2
  "owl.html", 2, 2
  "sparrow.html", 2, 2
  "hawk.html", 2, 2
  "magpie.html", 2, 2
  "nighthawk.html", 2, 2
  "gull.html", 2, 2
  "bluebird.html", 2, 2
  "raven.html", 2, 2
  "blackbird.html", 2, 2
  "sandpiper.html", 2, 2
  "pheasant.html", 2, 2
  "roadrunner.html", 2, 2
  "duck.html", 2, 2
  "vulture.html", 2, 2
  "pigeon.html", 2, 2
  "heron.html", 2, 2
  "stork.html", 2, 2
  "warbler.html", 2, 2
  "woodpecker.html", 2, 2
  "hummingbird.html", 2, 2
  "mallard.html", 2, 2
  "osprey.html", 2, 2
  "swallow.html", 2, 2
  "wren.html", 2, 2
  "goose.html", 2, 2
  "meadowlark.html", 2, 2
  "pelican.html", 2, 2
  "starling.html", 2, 2
  "loon.html", 2, 2
  "eagle.html", 2, 2
  "puffin.html", 2, 2
  "egret.html", 2, 2
  </code></pre>
  
  <p>Notice how after the occurrence counts and initial occurrence are determined to be the same it stops sorting.  Correct output would be:</p>
  
  <pre><code>c
  "index.html", 6, 2
  "cardinal.html", 3, 2
  "chickadee.html", 3, 2
  "crane.html", 3, 2
  "crow.html", 3, 2
  "cuckoo.html", 3, 2
  "albatross.html", 2, 2
  "blackbird.html", 2, 2
  "bluebird.html", 2, 2
  "dove.html", 2, 2
  "duck.html", 2, 2
  "eagle.html", 2, 2
  "egret.html", 2, 2
  "falcon.html", 2, 2
  "finch.html", 2, 2
  "goose.html", 2, 2
  "gull.html", 2, 2
  "hawk.html", 2, 2
  "heron.html", 2, 2
  "hummingbird.html", 2, 2
  "ibis.html", 2, 2
  "kingfisher.html", 2, 2
  "loon.html", 2, 2
  "magpie.html", 2, 2
  "mallard.html", 2, 2
  "meadowlark.html", 2, 2
  "mockingbird.html", 2, 2
  "nighthawk.html", 2, 2
  "osprey.html", 2, 2
  "owl.html", 2, 2
  "pelican.html", 2, 2
  "pheasant.html", 2, 2
  "pigeon.html", 2, 2
  "puffin.html", 2, 2
  "quail.html", 2, 2
  "raven.html", 2, 2
  "roadrunner.html", 2, 2
  "robin.html", 2, 2
  "sandpiper.html", 2, 2
  "sparrow.html", 2, 2
  "starling.html", 2, 2
  "stork.html", 2, 2
  "swallow.html", 2, 2
  "swan.html", 2, 2
  "tern.html", 2, 2
  "turkey.html", 2, 2
  "vulture.html", 2, 2
  "warbler.html", 2, 2
  "woodpecker.html", 2, 2
  "wren.html", 2, 2
  </code></pre>
  
  <p>Anyways, like I said I have to omit a lot of information, but if anything comes to mind i'd really appreciate ideas.  It is a fact that the "matches" data structure is not modified after the creation of the ArrayList.  </p>
  
  <p>EDIT:
  Hey, i'm still reading through your responses.  Thanks so much for the quick help!  Here is the entire IndexEntry Class since a few of you asked:</p>
  
  <pre><code>    class IndexEntry&lt;extends Comparable&lt;T&gt;&gt; implements Comparable&lt;IndexEntry&lt;T&gt;&gt;{
          private T theLocation;
          private Integer occurrenceCount;
          private Integer firstOccurrence;
  
          public IndexEntry(T location, Integer firstOcc, Integer occCount){
              theLocation = location;
              occurrenceCount = occCount;
              firstOccurrence = firstOcc;
          }
  
          public int compareTo(IndexEntry&lt;T&gt; other) {         
              if(this.occurrenceCount &lt; other.getOccurrenceCount()){
                  return 1;
              }
              else if(this.occurrenceCount &gt; other.getOccurrenceCount()){
                  return -1;
              }
              else{//Need to compare Initial Positions
                  if(this.firstOccurrence == other.getFirstOccurrence()){
                      return this.getLocationName().compareTo(other.getLocationName());
                  }
                  else if(this.firstOccurrence &gt; other.getFirstOccurrence()){
                      return 1;
                  }
                  else{
                      return -1;
                  }
              }
          }   
  
          public String getLocationName(){
              if(theLocation instanceof File){
                  return ((File) theLocation).getAbsolutePath();
              }
              else{
                  return theLocation.toString();
              }
          }
  
          public Integer getFirstOccurrence(){
              return firstOccurrence;
          }
  
          public Integer getOccurrenceCount(){
              return this.occurrenceCount;
          }
  
          public void addToOccurrenceCount(Integer toAdd){
              occurrenceCount += toAdd;
          }
  
          public void setFirstOccurrence(Integer firstOcc){
              firstOccurrence = new Integer(firstOcc);
          }
      }
  </code></pre>