Community
Participate
Working Groups
I'm currently using, although not perfect, but simple & workable: package org.eclipse.jface.sorters; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerSorter; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; /** * TableViewer Sorter. * * @author Administrator * */ public class TableViewerSorter extends ViewerSorter { //private int order = 1; // asc/desc private int columnIndex = 0; //private int direction = SWT.NONE; public TableViewerSorter(int columnIndex) { this.columnIndex = columnIndex; // asc/desc //order *= -1; } /* * public static void reverseOrder() { order *= -1; } */ public int compare(Viewer viewer, Object e1, Object e2) { if (viewer instanceof TableViewer) { TableViewer tv = (TableViewer) viewer; tv.getTable().setSortColumn(tv.getTable().getColumn(columnIndex)); int idx1 = -1, idx2 = -1; for (int i = 0; i < tv.getTable().getItemCount(); i++) { Object obj = tv.getElementAt(i); if (obj.equals(e1)) { idx1 = i; } else if (obj.equals(e2)) { idx2 = i; } if (idx1 > 0 && idx2 > 0) { break; } } int order = 0; if (idx1 > -1 && idx2 > -1) { String str1 = tv.getTable().getItems()[idx1].getText(this.columnIndex); String str2 = tv.getTable().getItems()[idx2].getText(this.columnIndex); order = str1.compareTo(str2); if (tv.getTable().getSortDirection() != SWT.UP) { order *= -1; } } return order; } return 0; } /** * table is bind to Sorter. * * @param tableViewer */ public static void bind(final TableViewer tableViewer) { for (int i = 0; i < tableViewer.getTable().getColumnCount(); i++) { final int columnNum = i; TableColumn column = tableViewer.getTable().getColumn(i); column.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { TableViewerSorter sorter = new TableViewerSorter(columnNum); Table table = tableViewer.getTable(); if (table.getSortDirection() == SWT.UP) { table.setSortDirection(SWT.DOWN); } else if (table.getSortDirection() == SWT.DOWN) { table.setSortDirection(SWT.UP); } else { table.setSortDirection(SWT.UP); } tableViewer.setSorter(sorter); } }); } } }
Is this a sorter that allows you to sort by a particular column? Generally table sorting is a little more complex with the sorting done with several columns in a particular order. tv.getTable().setSortColumn(tv.getTable().getColumn(columnIndex)) will also cause an update of the sort indicator everytime it is called. There also appears to be a large performance problem here for (int i = 0; i < tv.getTable().getItemCount(); i++) { Object obj = tv.getElementAt(i); if (obj.equals(e1)) { idx1 = i; } else if (obj.equals(e2)) { idx2 = i; } if (idx1 > 0 && idx2 > 0) { break; } } is o(N*N*lgN) or O(N*N*N*lgN) if hash lookup is not being used. (O)n lg(n) for the sort from the table 0(n) for for (int i = 0; i < tv.getTable().getItemCount(); i++) { and O(n) for tv.getElementAt(i);
It is a generic sorter, for sorting strings of a table(every column). Of course sorting by datatype each column is more better.
Hitesh is now responsible for watching bugs in the [Viewers] component area.
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.