title: Comparing an Array of Comparables in Java
body: <p>I want to compare an array of comparables. The simplest way seems the following (details not shown):</p>
  
  <pre><code>public class ArrayComparable implements Comparable&lt;ArrayComparable&gt;{
  ArrayList&lt;Comparable&lt;?&gt;&gt; list = new ArrayList&lt;Comparable&lt;?&gt;&gt;();
  
  @Override
  public int compareTo(ArrayComparable ac) {
      Iterator&lt;Comparable&lt;?&gt;&gt; itr = ac.list.iterator();
      for(Comparable&lt;?&gt; l : list) {
          Comparable&lt;?&gt; itrNext = itr.next();
          if(itrNext.getClass() == l.getClass()) {
              if(itrNext.compareTo(l)) {
                  //something
              } else {
                  //other stuff
              }
          } else {
              //some other thing
          }
      }
  }
  </code></pre>
  
  <p>Of course the problem here is that the <code>compareTo</code> as in <code>itrNext.compareTo(l)</code> will not work giving the error: <code>The method compareTo(capture#6-of ?) in the type Comparable&lt;capture#6-of ?&gt; is not applicable for the arguments (Comparable&lt;capture#7-of ?&gt;)</code>
  which I understand why (as far as the method is concerned I might be comparing apples to oranges). On the other hand, I know I am not as I check for the class of things before comparing them.</p>
  
  <p>So is there a way I can make this work? Don't worry about the sanity of comparing arrays of any comparables, as I have a good reason why I want to do that.</p>
  
  <p>EDIT- SO why would I want to do something like this. Say I wanted to have an array of comparables, and I didn't care what was contained in each index, as long as the types corresponded, and they could be compared. Then I could do a general lexicographical compare between these arrays. This way I don't have to write a comparable for <code>(int,int)</code> and <code>(int, string)</code>, and <code>(string, double, string)</code> or whatever you need. I just write one, and as long as I make sure that the types match (and I can), I am good to go.</p>