Google Tag Manager

2011/11/28

aterai

JTable group directories first sorting

Code

// > dir /O:GN
// > ls --group-directories-first
class FileGroupComparator extends DefaultFileComparator{
  private final JTable table;
  public FileGroupComparator(JTable table, int column) {
    super(column);
    this.table = table;
  }
  public int compare(File a, File b) {
    int flag = 1;
    java.util.List < ? extends TableRowSorter.SortKey > keys
        = table.getRowSorter().getSortKeys();
    if(!keys.isEmpty()) {
      TableRowSorter.SortKey sortKey = keys.get(0);
      if(sortKey.getColumn()==column &&
         sortKey.getSortOrder()==SortOrder.DESCENDING) {
        flag = -1;
      }
    }
    if(a.isDirectory() && !b.isDirectory()) {
      return -1*flag;
    }else if(!a.isDirectory() && b.isDirectory()) {
      return  1*flag;
    }else{
      return super.compare(a, b);
    }
  }
}

References