如何进行升序和降序排序。在下面的SWT代码中,升序成功运行,但是当我单击表头时,降序不起作用怎么办?

这是我的以下代码。

package test;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Collections;
 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), 0.000565529),
             new TableRow(2, "abc", new Date(1367784269000L),1.09E-06),
            new TableRow(3, "efc", new Date(1363584269000L),0.000273487),
            new TableRow(4, "ccc", new Date(1363734269000L),0.000170431),
           new TableRow(5, "ddd", new Date(1363734269000L),1.09E-06),
           new TableRow(6, "fff", new Date(1363734269000L),0.000273487),
           new TableRow(7, "ggg", new Date(1363734269000L),0.000170431),

};
private Table table;
private TableColumn intColumn;
private TableColumn strColumn;
private TableColumn dateColumn;
private TableColumn scientificColumn;

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

    scientificColumn = new TableColumn(table, SWT.NONE);
    scientificColumn.setText("scientificvalue");
    scientificColumn.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);
            if (column == scientificColumn) Arrays.sort(rows,BY_VAL );
            table.setSortColumn(column);
            updateTable();
        }
    };

    Listener reverseSortListener = new Listener() {
        public void handleEvent(Event e) {
            TableColumn column = (TableColumn) e.widget;
            if (column == intColumn) Arrays.sort(rows, Collections.reverseOrder());
            if (column == strColumn) Arrays.sort(rows, Collections.reverseOrder());
            if (column == dateColumn) Arrays.sort(rows, Collections.reverseOrder());
            if (column == scientificColumn) Arrays.sort(rows,Collections.reverseOrder() );
            table.setSortColumn(column);
            updateTable();
        }
    };
      //        intColumn.addListener(SWT.Selection, sortListener);
      //        strColumn.addListener(SWT.Selection, sortListener);
      //        dateColumn.addListener(SWT.Selection, sortListener);
      //        scientificColumn.addListener(SWT.Selection, sortListener);

    intColumn.addListener(SWT.Selection, reverseSortListener);
    strColumn.addListener(SWT.Selection, reverseSortListener);
    dateColumn.addListener(SWT.Selection, reverseSortListener);
    scientificColumn.addListener(SWT.Selection, reverseSortListener);

    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());
        item.setText(row.asDouble());
    }
}

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 double sciVal;

    private SimpleDateFormat format = new SimpleDateFormat();
  //        private   NumberFormat formatter = new DecimalFormat("###.#####");

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

    public String[] asString() {
        return new String[] {Integer.toString(val), str, format.format(date)};
    }
    public String[] asDouble() {
        return new String[] {Integer.toString(val), str, format.format(date),String.format("%.3E", sciVal)};
    }
}

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


}

最佳答案

Collections.reverseOrder()返回一个比较器,该比较器根据数组内容的自然顺序以相反的顺序对数组进行排序。这不是您此处需要的,因为您要对列进行排序。

而是使用接受比较器使用的reverseOrder版本。所以Collections.reverseOrder(BY_VAL)Collections.reverseOrder(BY_STR),...

09-16 05:37