DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//daten.....
table.setAutoCreateColumnsFromModel(false);
int mColIndex = 1;
boolean ascending = false;
sortColumn(model, mColIndex, ascending);
public void sortColumn(DefaultTableModel model, int colIndex, boolean ascending) {
Vector data = model.getDataVector();
Object[] colData = new Object[model.getRowCount()];
for (int i=0; i<colData.length; i++) {
colData[i] = ((Vector)data.get(i)).get(colIndex);
}
Arrays.sort(colData, new ColumnSorter(ascending));
for (int i=0; i<colData.length; i++) {
((Vector)data.get(i)).set(colIndex, colData[i]);
}
model.fireTableStructureChanged();
}
public class ColumnSorter implements Comparator {
boolean ascending;
ColumnSorter(boolean ascending) {
this.ascending = ascending;
}
public int compare(Object a, Object b) {
if (a instanceof String && ((String)a).length() == 0) {
a = null;
}
if (b instanceof String && ((String)b).length() == 0) {
b = null;
}
if (a == null && b == null) {
return 0;
} else if (a == null) {
return 1;
} else if (b == null) {
return -1;
} else if (a instanceof Comparable) {
if (ascending) {
return ((Comparable)a).compareTo(b);
} else {
return ((Comparable)b).compareTo(a);
}
} else {
if (ascending) {
return a.toString().compareTo(b.toString());
} else {
return b.toString().compareTo(a.toString());
}
}
}
}