http://www.javawiki.org/2006_01_18/tidy-up-your-jface-viewer-sorting-a-table/

Today I want to show how to add a sorter to your JFace-Table. The requirement is to sort descending und ascending by clicking on the TableColumn-Header.

The Viewer with an inital Sorting (Column ID)
The inital view with sorting

The ascending sorting after a click on the ID-Column header
The ascending sorting after a click on the ID-Column header.

JFace already provides sorting-functionality. We just have to provide something like an alogrithm to arrange the items. For that we implemented the CollectionSorter that uses the default Collator from ViewerSorter. Now look at the Sorter:

Initializing the Sorter with a default column

JAVA:
  1.  
  2. public class CollationSorter extends ViewerSorter {
  3.  
  4. private Map sortMap = new HashMap();
  5.  
  6. /**
  7. * Creates an instance of the sorter
  8. * @param tc0 the default sorter-column.
  9. */
  10. public CollationSorter(TableColumn defaultColumn) {
  11. setCurrentColumn(defaultColumn);
  12. }
  13.  
  14. /**
  15. * Pushs the current sortorder in a map which key is the
  16. * table-column.
  17. * @param column
  18. */
  19. public void pushSortCriteria(TableColumn column) {
  20. if (this.sortMap.get(column) == null) {
  21. this.sortMap.put(column,new Boolean(true));
  22. }
  23. else {
  24. boolean newSort = !((Boolean)this.sortMap.get(column)).booleanValue();
  25. this.sortMap.put(column,new Boolean(newSort));
  26. }
  27. }
  28.  
  29. /**
  30. * Asks for the current sort-order and inverts the sort-order
  31. * @param column the requested column
  32. * @return true if the sortIndex is descending, else false.
  33. */
  34. public boolean isDescending(TableColumn column) {
  35. boolean returnValue = true;
  36. if (this.sortMap.get(column) != null) {
  37. returnValue = ((Boolean)this.sortMap.get(column)).booleanValue();
  38. } else {
  39. pushSortCriteria(column);
  40. }
  41. return returnValue;
  42. }
  43.  
  44. private TableColumn currentColumn = null;
  45.  
  46. /* (non-Javadoc)
  47. * @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
  48. */
  49. public int compare(Viewer viewer, Object obj1, Object obj2) {
  50. int rc = -1;
  51. // get the data
  52. AbstractBaseElement data1 = (AbstractBaseElement) obj1;
  53. AbstractBaseElement data2 = (AbstractBaseElement) obj2;
  54.  
  55. CollationKey key1 = null;
  56. CollationKey key2 = null;
  57.  
  58. if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(0)) {
  59. key1 = getCollator().getCollationKey(data1.getId());
  60. key2 = getCollator().getCollationKey(data2.getId());
  61.  
  62. }
  63. else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(1)){
  64. key1 = getCollator().getCollationKey(data1.getName());
  65. key2 = getCollator().getCollationKey(data2.getName());
  66. }
  67. else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(2)){
  68. key1 = getCollator().getCollationKey(data1.getDescription());
  69. key2 = getCollator().getCollationKey(data2.getDescription());
  70. }
  71. // replace null-strings with empty-strings
  72. if (key1 == null)
  73. key1 = getCollator().getCollationKey(""); //$NON-NLS-1$
  74.  
  75. if (key2 == null)
  76. key2 = getCollator().getCollationKey(""); //$NON-NLS-1$
  77.  
  78. if (isDescending(this.currentColumn)) {
  79. rc = key1.compareTo(key2);
  80. }
  81. else {
  82. rc = key2.compareTo(key1);
  83. }
  84. return rc;
  85. }
  86.  
  87. /**
  88. * Sets the sort column.
  89. * @param currentColumn The currentColumn to set.
  90. */
  91. public void setCurrentColumn(TableColumn currentColumn) {
  92. this.currentColumn  = currentColumn;
  93. pushSortCriteria(currentColumn);
  94. }
  95. }
  96.  

This sorter you can use everywhere, you just have to modify the Creation of the Collator-Keys.

Direction Indicator
This feature will be aviable in Eclipse 3.2

Download the JFace TableSorter Plugin (Requires Model-Plugin)
Download the JFace TableSorter RCP (source included)