package example;

import java.awt.*;
import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    String[] empty = {"", ""};
    String[] columnNames = {"DefaultTableRowSorter", "EmptiesLastTableRowSorter"};
    Object[][] data = {
      {"aaa", "aaa"}, {"ddd", "ddd"},
      {"bbb", "bbb"}, {"eee", "eee"},
      {"ccc", "ddd"}, {"fff", "fff"},
      empty, empty, empty, empty, empty
    };
    TableModel model = new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return String.class;
      }
    };
    JTable table = new JTable(model);
    table.setAutoCreateRowSorter(true);
    TableRowSorter<? extends TableModel> sorter = (TableRowSorter<? extends TableModel>) table.getRowSorter();
    sorter.setComparator(1, new RowComparator(table, 1));

    add(new JScrollPane(table));
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class RowComparator implements Comparator<String>, Serializable {
  private static final long serialVersionUID = 1L;
  protected final int column;
  private final JTable table;

  protected RowComparator(JTable table, int column) {
    this.table = table;
    this.column = column;
  }

  @Override public int compare(String a, String b) {
    int flag = 1;
    List<? extends RowSorter.SortKey> keys = table.getRowSorter().getSortKeys();
    if (!keys.isEmpty()) {
      RowSorter.SortKey sortKey = keys.get(0);
      if (sortKey.getColumn() == column && sortKey.getSortOrder() == SortOrder.DESCENDING) {
        flag = -1;
      }
    }
    if (a.isEmpty() && !b.isEmpty()) {
      return flag;
    } else if (!a.isEmpty() && b.isEmpty()) {
      return -1 * flag;
    } else {
      return a.compareTo(b);
    }
  }
}

@@ -501,16 +501,18 @@
 %7B%0A      
+  
 %7B%22aaa%22, 
@@ -531,24 +531,26 @@
 d%22, %22ddd%22%7D,%0A
+  
       %7B%22bbb%22
@@ -581,16 +581,18 @@
 ,%0A      
+  
 %7B%22ccc%22, 
@@ -611,24 +611,26 @@
 f%22, %22fff%22%7D,%0A
+  
       empty,
@@ -917,21 +917,16 @@
 e);%0A    
-Table
 RowSorte
@@ -962,14 +962,17 @@
 r = 
-(T
+t
 able
+.get
 RowS
@@ -980,69 +980,77 @@
 rter
-%3C? extends TableModel%3E) table.getRowSorter();%0A    sorter.setC
+();%0A    if (sorter instanceof TableRowSorter) %7B%0A      RowComparator c
 ompa
@@ -1058,11 +1058,10 @@
 ator
-(1,
+ =
  new
@@ -1084,18 +1084,107 @@
 able, 1)
-)
 ;
+%0A      ((TableRowSorter%3C? extends TableModel%3E) sorter).setComparator(1, comparator);%0A    %7D
 %0A%0A    ad

add instanceof check