How do I realize sorting of columns containing. I set Cloumnclass is Number.Class
public Class<?> getColumnClass(int columnIndex) {
return Number.class;
}
and Create TableRowSorter
TableRowSorter sorter= new TableRowSorter<TableModel>(table_mode);
table.setRowSorter(sorter);
The result 8, 80, 9, 989
instead 989 , 80, 9, 8
From the documentation of TableRowSorter:
TableRowSorter
usesComparator
s for doing comparisons. The following defines how aComparator
is chosen for a column:
- If a
Comparator
has been specified for the column by thesetComparator
method, use it.- If the column class as returned by
getColumnClass
isString
, use theComparator
returned byCollator.getInstance()
.- If the column class implements
Comparable
, use a Comparator that invokes thecompareTo
method.- If a
TableStringConverter
has been specified, use it to convert the values toString
s and then use theComparator
returned byCollator.getInstance()
.- Otherwise use the
Comparator
returned byCollator.getInstance()
on the results from callingtoString
on the objects.
The third and fifth rule are the cause of your issue: You are returning Number.class, which does not implement Comparable. Therefore, your table is sorting using the fifth rule: your values are being treated as Strings.
Instead of returning Number.class, you need to return something which actually implements Comparable, such as Integer.class, Double.class, or BigDecimal.class. The javadoc of each class will tell you what interfaces it implements.
Alternatively, you could install a custom Comparator on the table column, but your Comparator will have to do the work of casting the values and possibly converting them. Returning a Comparable class is much easier.