4

在 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);
4

2 回答 2

9

我已经用排序修改了 SWT 片段,以显示如何对具有不同数据类型的列进行排序。

/*
 * Table example snippet: sort a table by column
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SortTable {
    private TableRow rows[] = new TableRow[] {
            new TableRow(1, "aaa", new Date(1363784269000L)),
            new TableRow(2, "abc", new Date(1367784269000L)),
            new TableRow(3, "efc", new Date(1363584269000L)),
            new TableRow(4, "ccc", new Date(1363734269000L)),
        };
    private Table table;
    private TableColumn intColumn;
    private TableColumn strColumn;
    private TableColumn dateColumn;

    public SortTable() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        table = new Table(shell, SWT.BORDER);
        table.setHeaderVisible(true);
        intColumn = new TableColumn(table, SWT.NONE);
        intColumn.setText("int");
        intColumn.setWidth(50);
        strColumn = new TableColumn(table, SWT.NONE);
        strColumn.setText("string");
        strColumn.setWidth(50);
        dateColumn = new TableColumn(table, SWT.NONE);
        dateColumn.setText("date");
        dateColumn.setWidth(100);

        updateTable();

        Listener sortListener = new Listener() {
            public void handleEvent(Event e) {
                TableColumn column = (TableColumn) e.widget;
                if (column == intColumn) Arrays.sort(rows, BY_VAL);
                if (column == strColumn) Arrays.sort(rows, BY_STR);
                if (column == dateColumn) Arrays.sort(rows, BY_DATE);
                table.setSortColumn(column);
                updateTable();
            }
        };
        intColumn.addListener(SWT.Selection, sortListener);
        strColumn.addListener(SWT.Selection, sortListener);
        dateColumn.addListener(SWT.Selection, sortListener);
        shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    private void updateTable() {
        table.removeAll();
        for (TableRow row : rows) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(row.asString());
        }
    }

    public final Comparator<TableRow> BY_VAL = new Comparator<TableRow>() {
        @Override
        public int compare(TableRow o1, TableRow o2) {
            if (o1.val < o2.val) return -1;
            if (o1.val > o2.val) return 1;
            return 0;
        }
    }; 

    public final Comparator<TableRow> BY_STR = new Comparator<TableRow>() {
        @Override
        public int compare(TableRow o1, TableRow o2) {
            return o1.str.compareTo(o2.str);
        }
    }; 

    public final Comparator<TableRow> BY_DATE = new Comparator<TableRow>() {
        @Override
        public int compare(TableRow o1, TableRow o2) {
            return o1.date.compareTo(o2.date);
        }
    };

    private class TableRow {
        private int val;
        private String str;
        private Date date;
        private SimpleDateFormat format = new SimpleDateFormat();


        public TableRow(int val, String str, Date date) {
            this.val = val;
            this.str = str;
            this.date = date;
        }

        public String[] asString() {
            return new String[] {Integer.toString(val), str, format.format(date)};
        }
    }

    public static void main(String[] args) {
        new SortTable();
    }
}

总结一下,如何对表中的列进行排序完全取决于程序员,因为表总是想要一行字符串数组,不管它们是什么意思。

于 2013-03-20T13:19:00.353 回答
1

您可以将列的比较器作为数据放入列,然后使用它进行排序。这使排序逻辑更接近于列本身的定义。

intColumn = new TableColumn(table, SWT.NONE);
intColumn.setText("int");
intColumn.setWidth(50);
intColumn.setData(new Comparator<TableItem>() {
    @Override
    public int compare(TableItem t1, TableItem t2) {
        int i1 = Integer.parseInt(t1.getText(0));
        int i2 = Integer.parseInt(t2.getText(0));
        if (i1.val < i2.val) return -1;
        if (i1.val > i2.val) return 1;
        return 0;
    }
};
strColumn = new TableColumn(table, SWT.NONE);
strColumn.setText("string");
strColumn.setWidth(50);
strColumn.setData(new Comparator<TableItem>() {
    @Override
    public int compare(TableItem t1, TableItem t2) {
        return t1.getText(1).compareTo(t2.getText(1));
    }
};
dateColumn = new TableColumn(table, SWT.NONE);
dateColumn.setText("date");
dateColumn.setWidth(100);
dateColumn.setData(new Comparator<TableItem>() {
    @Override
    public int compare(TableItem t1, TableItem t2) {
        return Date.parse(t1.getText(2)).compareTo(Date.parse(t2.getText(2)));
    }
};

Listener sortListener = e -> {
    TableColumn sortColumn = table.getSortColumn();
    TableColumn selectedColumn = (TableColumn) e.widget;
    int dir = table.getSortDirection();
    if (sortColumn == selectedColumn) {
        dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
    } else {
        table.setSortColumn(selectedColumn);
        dir = SWT.UP;
    }
    TableItem[] items = table.getItems();
    final Comparator<TableItem> comparator = (Comparator<TableItem>) selectedColumn.getData();
    for (int i = 1; i < items.length; i++) {
        for (int j = 0; j < i; j++) {
            if ((comparator.compare(items[i], items[j]) < 0 && dir == SWT.UP) || (comparator.compare(items[i], items[j]) > 0 && dir == SWT.DOWN)) {
                String[] oldItem = new String[table.getColumnCount()];
                for (int h = 0; h < table.getColumnCount(); h++) {
                    item[h] = items[i].getText(h);
                }
                items[i].dispose();
                TableItem newItem = new TableItem(table, SWT.NONE, j);
                newItem.setText(oldItem);
                items = table.getItems();
                break;
            }
        }
    }
    table.setSortDirection(dir);
};
于 2016-04-18T15:52:00.927 回答