title: Java - A TreeSet containing Lists of Integers?
body: <p>I need a Set that contains a List of Integers. Each List contains 2 int values (for x and y coordinates) and I want to be able to implement some sort of compare method so that the Set is sorted depending on the first value (X value) for the List in ascending order so that Lists with the first value being very large will appear at the tail of the Set.</p>
  
  <p>So that instead of printing out:</p>
  
  <pre><code>[2, 1]
  [1, 2]
  [3, 3]
  [2, 3]
  </code></pre>
  
  <p>It prints out:</p>
  
  <pre><code>[1, 2] 
  [2, 1]
  [2, 3]
  [3, 3]
  </code></pre>
  
  <p>The problem is that I get an error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: 
  java.util.Arrays$ArrayList cannot be cast to java.lang.Comparable
  </code></pre>
  
  <p>I honestly do not have much experience with sorting like this as I am a beginner. Any help would be brilliant, thanks!</p>
  
  <p>Code so far:</p>
  
  <pre><code>public class Shape implements Comparator&lt;List&lt;Integer&gt;&gt; {
      private SortedSet&lt;List&lt;Integer&gt;&gt; coords;
  
      @Override
      public int compare(List&lt;Integer&gt; l1, List&lt;Integer&gt; l2) {
          if (l1.get(0) &gt; l2.get(0))
              return 1;
          else
              return -1;
      }
  
      public Shape(int shapeID) {
          coords = new TreeSet&lt;List&lt;Integer&gt;&gt;();        
          switch (shapeID) {
              case 1:
                  coords.add(Arrays.asList(0, 2));
                  coords.add(Arrays.asList(1, 2));
                  coords.add(Arrays.asList(3, 2));
                  break;
              case 2:
                  coords.add(Arrays.asList(1, 1));
                  coords.add(Arrays.asList(1, 2));
                  coords.add(Arrays.asList(2, 1));
                  break;
              case 3:
                  coords.add(Arrays.asList(1, 3));
                  coords.add(Arrays.asList(2, 1));
                  coords.add(Arrays.asList(2, 3));
                  break;            
          }
          coords.add(Arrays.asList(2, 2));
      }
  
      public void rotate() {
          Iterator&lt;List&lt;Integer&gt;&gt; it = coords.iterator();
          // should print out a sorted Set:
          while (it.hasNext()) {          
              System.out.println(it.next());
              // do more stuff here..
          }
      }
  
      public static void main(String[] args) {
          Shape s = new Shape(2);
          s.rotate();
  
      }
  }
  </code></pre>