2011-05-18 96 views
0

由於我使用的是Java 1.4.2,這意味着我無法使用Java的表分類器實現。相反,我一直在使用從以前的答覆TableSorter.java類我以前的帖子了: Heads up on implementing rowsorter and rowfilter java 1.4TableSorter數值排序

它有一個問題,但是可以正常使用,這是它正確不數值進行排序。舉例來說,我有一個數字按以下順序在我的表: 5,18,9,7,2,33

一個遞增的順序排序會在我的JTable中顯示他們是這樣的:

18,2 ,33,5,7,9

遞減順序排序會在我的JTable中這樣顯示出來: 9,7,5,33,2,18

我不知道你是否已經意識到它,但顯然,數值的排序只是基於第一個數字而發生。

你有任何問題的快速解決?請記住,這些數值在我的JTable中用作getValue()方法所建議的字符串。

回答

0

要更新你對這個傢伙,我所做的就是從我原來的職位與此替換TableSorter.java的LEXICAL_COMPARATOR:

public static final Comparator LEXICAL_COMPARATOR = new Comparator() { public int compare(Object o1, Object o2) { if(o1 instanceof Integer) {
Integer firstNumeric, secondNumeric;

  firstNumeric =(Integer)o1; 
      secondNumeric= (Integer)o2; 

        return firstNumeric.compareTo(secondNumeric); 


     } 
     else 
     return o1.toString().compareTo(o2.toString()); 
    } 
}; 

現在,在getValueAt,請確保您通過簡單地將「int」值轉換爲Integer: new Integer(intValue);

我希望這會爲人們節省一些時間。

1

您需要實施一個Comparator執行數字比較,而不是默認的字典。因此,像(從http://crossedlogic.blogspot.com/2008/11/java-custom-comparators-sorting-text.html拍攝):

Comparator c1 = new java.util.Comparator() { 
     /** 
     * Custom compare to sort numbers as numbers. 
     * Strings as strings, with numbers ordered before strings. 
     * 
     * @param o1 
     * @param o2 
     * @return 
     */ 
@Override 
      public int compare(Object oo1, Object oo2) { 
       boolean isFirstNumeric, isSecondNumeric; 
       String o1 = oo1.toString(), o2 = oo2.toString(); 


      isFirstNumeric = o1.matches("\\d+"); 
      isSecondNumeric = o2.matches("\\d+"); 

      if (isFirstNumeric) { 
       if (isSecondNumeric) { 
        return Integer.valueOf(o1).compareTo(Integer.valueOf(o2)); 
       } else { 
        return -1; // numbers always smaller than letters 
       } 
      } else { 
       if (isSecondNumeric) { 
        return 1; // numbers always smaller than letters 
       } else { 
        isFirstNumeric = o1.split("[^0-9]")[0].matches("\\d+"); 
        isSecondNumeric = o2.split("[^0-9]")[0].matches("\\d+"); 

        if (isFirstNumeric) { 
         if (isSecondNumeric) { 
          int intCompare = Integer.valueOf(o1.split("[^0-9]")[0]).compareTo(Integer.valueOf(o2.split("[^0-9]")[0])); 
          if (intCompare == 0) { 
           return o1.compareToIgnoreCase(o2); 
          } 
          return intCompare; 
         } else { 
          return -1; // numbers always smaller than letters 
         } 
        } else { 
         if (isSecondNumeric) { 
          return 1; // numbers always smaller than letters 
         } else { 
          return o1.compareToIgnoreCase(o2); 
         } 
        } 
       } 
      } 
     } 
    }; 

然後傳遞,在作爲比較的列:

tblSorter.setColumnComparator(String.class, c1); 

讓我知道如何工作的。

+0

你也可以使用Apache commons的編號utils [NumberUtils.isNumber(o1)](http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/math/NumberUtils.html #isNumber(java.lang.String))如果你喜歡像我這樣的事情。 – Dave 2011-05-18 22:53:32

+0

費米感謝您的快速回復隊友。 那麼如何將這個添加到TableSorter中,我之前提到過這個帖子?我真的需要一個在這個.. – 2011-05-18 23:14:09

+0

Btw Femi,您提供的代碼只適用於Java 1.5 +。我僅限於使用java 1.4.2 ... – 2011-05-18 23:34:54

2

確認getColumnClass()返回數字類型,如Number

附錄:MyTableModel in TableSorterDemo就是一個例子。第三列的數據類型爲Integer,其子類爲Number;因此,getColumnClass()返回Integer.class。由於Integer實現了Comparable,因此該列按數字排序。

+0

+1,保持簡單。不需要自定義代碼。 – camickr 2011-05-19 00:08:56