title: JTable Keep Group of Rows always first when sorting ASC or DESC
body: <p>I Have a JTable I am using to show some data, the data consists basically of (Key,Value) pairs.</p>
  <p>I enable sorting of the Table by the Key Column.</p>
  <p>But I need a group of keys to always be at the top no matter if it is sorted ascending or descending.</p>
  <p>Exameple :</p>
  <p><strong>Unsorted</strong></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Key A</td>
  <td>Value A</td>
  </tr>
  <tr>
  <td>Key C</td>
  <td>Value C</td>
  </tr>
  <tr>
  <td>Key B</td>
  <td>Value B</td>
  </tr>
  <tr>
  <td>Key 1</td>
  <td>Value 1</td>
  </tr>
  <tr>
  <td>Key 3</td>
  <td>Value 3</td>
  </tr>
  <tr>
  <td>Key 2</td>
  <td>Value 2</td>
  </tr>
  <tr>
  <td>Key 4</td>
  <td>Value 4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p><strong>Sorted ASC</strong></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Key A</td>
  <td>Value A</td>
  </tr>
  <tr>
  <td>Key B</td>
  <td>Value B</td>
  </tr>
  <tr>
  <td>Key C</td>
  <td>Value C</td>
  </tr>
  <tr>
  <td>Key 1</td>
  <td>Value 1</td>
  </tr>
  <tr>
  <td>Key 2</td>
  <td>Value 2</td>
  </tr>
  <tr>
  <td>Key 3</td>
  <td>Value 3</td>
  </tr>
  <tr>
  <td>Key 4</td>
  <td>Value 4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p><strong>Sorted DESC</strong></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Key C</td>
  <td>Value C</td>
  </tr>
  <tr>
  <td>Key B</td>
  <td>Value B</td>
  </tr>
  <tr>
  <td>Key A</td>
  <td>Value A</td>
  </tr>
  <tr>
  <td>Key 4</td>
  <td>Value 4</td>
  </tr>
  <tr>
  <td>Key 3</td>
  <td>Value 3</td>
  </tr>
  <tr>
  <td>Key 2</td>
  <td>Value 2</td>
  </tr>
  <tr>
  <td>Key 1</td>
  <td>Value 1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I created a rowsorter with a comparator and was able to get the Sorted ASC,</p>
  <pre><code>final TableRowSorter&lt;CTableModel&gt; sorter = new TableRowSorter&lt;CTableModel&gt;((CTableModel) this.getModel());
  sorter.setComparator(0, new CJTableComparator());
  sorter.setSortable(1, false);
  </code></pre>
  <p>but the defaultrowsorter orders the desc by multiplying the row numbers by -1.</p>
  <pre><code>private int compare(int model1, int model2) {
  ...    
      if (sortOrder == SortOrder.DESCENDING) {
          result *= -1;
      }
  </code></pre>
  <p>and seeing how the method is private I cannot override it.</p>
  <p>Does anyone have any ideas on how I could handle this?</p>
  <pre><code>class CJTableComparator implements Comparator {
  
      @Override
      public int compare(Object o1, Object o2) {
  
          CTableModel model = ((CTableModel)CJTable.this.getModel());
  
          String so1 = o1.toString();
          String so2 = o2.toString();
  
          boolean isHeader1 = model.isHeader(so1);
          boolean isHeader2 = model.isHeader(so2);
  
  
          if(isHeader1 == isHeader2){
  
          }else if(isHeader1){
              so1 = &quot;0&quot;+so1;
              so2 = &quot;9&quot;+so2;
          }else if(isHeader2){
              so1 = &quot;9&quot;+so1;
              so2 = &quot;0&quot;+so2;
          }
  
          return so1.toString().compareToIgnoreCase(so2.toString());
      }
  }
  </code></pre>
  <p>Table Model</p>
  <pre><code>public class CTableModel extends DefaultTableModel {
  
      private final HashMap&lt;String, CTableModelRow&gt; rowsbyId = new HashMap&lt;String, CTableModelRow&gt;();
  
      private final HashMap&lt;String, CTableModelRow&gt; rowsHeaders = new HashMap&lt;String, CTableModelRow&gt;();
  
      static Object[][] empty = {{}};
  
      private static final String[] DEFALT_COLUMNS = {&quot;TAG&quot;, &quot;VALUE&quot;, &quot;PREV&quot;};
  
      public CTableModel (){
          super(empty,DEFALT_COLUMNS);
          this.setRowCount(0);
      }
  
      public CTableModelRow getRowByNumber(int number){
          return rowsbyNumber.get(number);
      }
  
      public boolean isHeader(String key){
          return rowsHeaders.containsKey(key);
      }
  }
  </code></pre>
  <p>END.</p>