我在SWT表的表头上添加了一个侦听器以对其进行排序。但是它仅适用于第一列和第二列。
Listener sortListener = new Listener() {
public void handleEvent(Event e) {
TableItem[] items = table.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
TableColumn column = (TableColumn) e.widget;
int index = column == tblclmnName ? 0 : 1;
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(index);
System.out.print(value1);
for (int j = 0; j < i; j++) {
String value2 = items[j].getText(index);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0), items[i].getText(1), items[i].getText(2), items[i].getText(3),
items[i].getText(4), items[i].getText(5)};
items[i].dispose();
TableItem item = new TableItem(table, SWT.NONE, j);
item.setText(values);
items = table.getItems();
break;
}
}
}
table.setSortColumn(column);
}
};
tblclmnName.addListener(SWT.Selection, sortListener);
tblclmnLevel.addListener(SWT.Selection, sortListener);
tblclmnApplication.addListener(SWT.Selection, sortListener);
tblclmnDate.addListener(SWT.Selection, sortListener);
tblclmnTime.addListener(SWT.Selection, sortListener);
tblclmnAssignment.addListener(SWT.Selection, sortListener);
table.setSortColumn(tblclmnName);
table.setSortDirection(SWT.UP);
我该如何按升序和降序排序?