在 swt 表中,排序适用于字符串,如何对整数、双精度和日期值进行排序。这仅适用于字符串升序。任何人都可以提出更好的方法来做到这一点。
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "1", "v", "1.1", "20/03/2013" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "10", "z", "1.5", "20/04/2013" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "3", "a", "1.3", "30/01/2013" });
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 == tblclmnNumber ? 0 : 1;
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(index);
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].dispose();
TableItem item = new TableItem(table, SWT.NONE, j);
item.setText(values);
items = table.getItems();
break;
}
}
}
table.setSortColumn(column);
}
};
tblclmnNumber.addListener(SWT.Selection, sortListener);
tblclmnName.addListener(SWT.Selection, sortListener);
tblclmnDeci.addListener(SWT.Selection, sortListener);
tblclmnDate.addListener(SWT.Selection, sortListener);
table.setSortColumn(tblclmnNumber);
table.setSortDirection(SWT.UP);