Bug 158112 - [Viewers] TableViewer have ITableLabelProvider interface, but dosen't have TableViewerSorter.
Summary: [Viewers] TableViewer have ITableLabelProvider interface, but dosen't have Ta...
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows 2000
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-21 04:17 EDT by xy CLA
Modified: 2019-09-06 16:11 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description xy CLA 2006-09-21 04:17:10 EDT
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);
    }
   });
  }
 }

}
Comment 1 Tod Creasey CLA 2006-09-22 08:41:45 EDT
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);
Comment 2 xy CLA 2006-09-24 20:11:41 EDT
It is a generic sorter, for sorting strings of a table(every column). Of course sorting by datatype each column is more better. 

Comment 3 Boris Bokowski CLA 2009-11-26 09:55:03 EST
Hitesh is now responsible for watching bugs in the [Viewers] component area.
Comment 4 Eclipse Webmaster CLA 2019-09-06 16:11:38 EDT
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.