title: setAutoCreateRowSorter with Objects
body: <p>I have a <code>JTable</code> with 3 columns. Each column has its own format and the table looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/ODoxG.png" alt="enter image description here"></p>
  
  <p>The problem is (as you can see) that the sorting is incorrect (<code>setAutoCreateRowSorter</code>).</p>
  
  <p>I tried to define my own Object type for <code>col3</code> that <code>implements Comparable</code> and also have a <code>toString()</code> method for this Object. But it seems like this doesn't help me to get the sorting correct.</p>
  
  <p>Any idea what I am doing wrong?</p>
  
  <pre><code>public class SortJTable {
  
      public static void main(String[] args) {
          String[] columns = getTableColumns();
          Object[][] tableData = getTableValues();
          TableModel model = new DefaultTableModel(tableData, columns) {
  
              @Override
              public Class getColumnClass(int col) {
                  if (col == 2) // third column is a TablePercentValue
                      return TablePercentValue.class;
                  else
                      return String.class;
              }
          };
  
          JTable table = new JTable(model);
          table.setAutoCreateRowSorter(true); // Make it possible to column-sort
  
          JFrame frame = new JFrame();
          frame.add(new JScrollPane(table));
          frame.pack();
          frame.setVisible(true);
      }
  
      private static String[] getTableColumns(){
          String[] columns = new String[3];
          columns[0] = "col1";
          columns[1] = "col2";
          columns[2] = "col3";
          return columns;
      }
  
      private static Object[][] getTableValues(){
          Object[][] tableData = new Object[100][3];
          for(int i=0; i&lt;tableData.length; i++){
              for(int j=0; j&lt;tableData[0].length; j++){
                  String value;
                  if(j==2)
                      value = i+","+j+"%";
                  else if(j == 1)
                      value = i+":"+j;
                  else
                      value = i+""+j;
                  tableData[i][j] = value;
              }
          }
          return tableData;
      }
  }
  
  class TablePercentValue implements Comparable&lt;TablePercentValue&gt; {
  
      private String value;
      private double compValue;
  
      public TablePercentValue(String value){
          this.value = value;
          // Remove "%"-sign and convert to double value
          compValue = Double.parseDouble(value.replace("%", ""));
      }
  
      public String toString(){
          return value;
      }
  
      @Override
      public int compareTo(TablePercentValue o) {
          return compValue&gt;o.compValue ? 1 : -1;
      }
  }
  </code></pre>