开发者

How can i sort my JTable using Bubble sort?

开发者 https://www.devze.com 2023-02-22 20:48 出处:网络
I would like to know how to implement Bubble sort onto a DefaultTableModel. (I know there is an auto sorter but i have to use bubble sort.) I already know how to use bubble sort but don\'t know to how

I would like to know how to implement Bubble sort onto a DefaultTableModel. (I know there is an auto sorter but i have to use bubble sort.) I already know how to use bubble sort but don't know to how implement onto DefaultTableModel. I was thinking about first getting the row values and storing the values into a String[] then sorting the String[] and then converting it back into a String[][] and then putting it back into the TableModel. Is there any faster way to do this?

EDIT: Is there 开发者_JS百科any better way to do this? Still implementing bubble sort though?


The only reason for using bubble sort on a table model (and not on an external array) would be that we can observe the sorting process by looking at the table.

so, use the DefaultTableModel's setValueAt and getValueAt methods for your comparison and swappings. Here is an example.

class DTMSlowSorter {
   /**
    */
   private DefaultTableModel model;
   /**
    * the number of the column by which we want to sort
    */
   private int sortColNum;
   /**
    * the comparator for the elements.
    */
   private Comparator comparator;

   /**
    * The time to sleep between two sorting steps.
    */
   private int sleepTime = 500;

   /**
    * swaps the contents of two rows.
    */
   void swap(final int rowA, final int rowB) {
        try {
            EventQueue.invokeAndWait(new Runnable(){public void run() {
                int colCount = model.getColumnCount();
                Object[] temp = new Object[colCount];
                for(int i = 0; i < colCount; i++) {
                   temp[i] = model.getValueAt(rowA, i);
                }
                for(int i = 0; i < colCount; i++) {
                   model.setValueAt(model.getValueAt(rowB, i), rowA, i);
                }
                for(int i = 0; i < colCount; i++) {
                   model.setValueAt(temp[i], rowA, i);
                }
            }});
           Thread.sleep(sleepTime);
        } catch(InterruptedException ex) { ex.printStackTrace();}
   }

   /**
    * compares two rows.
    * @returns
    *      -1 if  A < B
    *       0 if  A = B
    *       1 if  A > B
    */
   int compare(int rowA, int rowB) {
       Object valA = model.getValueAt(rowA, sortColNum);
       Object valB = model.getValueAt(rowB, sortColNum);
       if(comparator != null) {
          return comparator.compare(valA, valB);
       }
       else {
          return ((comparable)valA).compareTo(valB);
       }
   }

   public void sort() {
      // here your bubblesort implementation, using compare and swap.
   }

}

(I hope I didn't do your whole homework here, but at least you'll have to implement the sorting yourself.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号