Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6300059

javax.swing.DefaultRowSorter.setComparator/sort methods behaviour is not clear

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      windows_xp

      Description

      While using Comparator in DefaultRowSorter I've faced with a problem : if I sort in the SortOrder.ASCENDING order and in the SortOrder.DESCENDING order I get the following output:
      ================================
      ASCENDING
      row index in terms of Model: 0; in view: = 0
      row index in terms of Model: 1; in view: = 4
      row index in terms of Model: 2; in view: = 3
      row index in terms of Model: 3; in view: = 2
      row index in terms of Model: 4; in view: = 1
      DESCENDING
      row index in terms of Model: 0; in view: = *4*
      row index in terms of Model: 1; in view: = *0*
      row index in terms of Model: 2; in view: = *1*
      row index in terms of Model: 3; in view: = *2*
      row index in terms of Model: 4; in view: = *3*
      ==================================
      But I've expected :
      ===================================
      ASCENDING
      row index in terms of Model: 0; in view: = 0
      row index in terms of Model: 1; in view: = 4
      row index in terms of Model: 2; in view: = 3
      row index in terms of Model: 3; in view: = 2
      row index in terms of Model: 4; in view: = 1
      DESCENDING
      row index in terms of Model: 0; in view: = *1*
      row index in terms of Model: 1; in view: = *2*
      row index in terms of Model: 2; in view: = *3*
      row index in terms of Model: 3; in view: = *4*
      row index in terms of Model: 4; in view: = *0*
      ==================================
       To reproduce it, invoke the following code:
      //-------------------------------------------------------------------------
      import javax.swing.table.TableRowSorter;
      import javax.swing.table.TableModel;
      import javax.swing.table.AbstractTableModel;
      import javax.swing.*;
      import java.util.Comparator;
      import java.util.LinkedList;
      import java.util.List;

      public class TestComparator {
      class ZeroComparator implements Comparator<Integer> {
      public int compare(Integer i1, Integer i2) {
      if (i1 == 0) i1 = 100;
      if (i2 == 0) i2 = 100;
      return i2 - i1;
      }
      }

      private TableModel dataModel = new AbstractTableModel() {
      public int getColumnCount() {
      return 3;
      }

      public int getRowCount() {
      return 5;
      }

      public Object getValueAt(int row, int col) {
      return row * col;
      }
      };
      private DefaultRowSorter rs = new TableRowSorter(dataModel);
      private List<RowSorter.SortKey> keys = new LinkedList<RowSorter.SortKey>();

      public void test() {
      rs.setComparator(1, new ZeroComparator());

      System.out.println("ASCENDING");
      keys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
      rs.setSortKeys(keys);
      printRowNumbers();

      System.out.println("DESCENDING");
      keys.clear();
      keys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
      rs.setSortKeys(keys);
      printRowNumbers();

      }

      private void printRowNumbers() {
      for (int i = 0; i < dataModel.getRowCount(); i++) {
      System.out.println("row in Model: " + i +
      "; in view: = " + rs.convertRowIndexToView(i));
      }
      }

      public static void main(String[] args) {
      TestComparator tc= new TestComparator();
      tc.test();
      }
      }
      //--------------------------------------------------------------------------

      Such behaviour seems to be strange. Could you, please, explain if I wrong.

      ###@###.### 2005-07-21 15:26:54 GMT

        Attachments

          Activity

            People

            Assignee:
            svioletsunw Scott Violet (Inactive)
            Reporter:
            ynovozhi Yulia Novozhilova (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: