Since I’m using Java 1.4.2, this means that I cannot use Java’s implementation of the table sorter. Instead, I’ve been using the TableSorter.java class from an earlier reply to my previous post:
Heads up on implementing rowsorter and rowfilter java 1.4
It’s working perfectly with one problem however, which is that it doesn’t sort numeric values correctly. For instance, I have the following sequence of numbers in my table:
5,18,9,7,2,33
A increasing order sorting would display them like this in my JTable:
18,2,33,5,7,9
A decreasing order sorting would display them like this in my JTable:
9,7,5,33,2,18
I don’t know if you have realized it, but apparently, the sorting of numeric values is only happening based on the first digit.
Do you have any quick fix for the problem? Please bare in mind that those numeric values are used as strings in my JTable as suggested by the getValue() method.
To update you guys on this, what I did was to replace the LEXICAL_COMPARATOR in the TableSorter.java from my original post with this:
public static final Comparator LEXICAL_COMPARATOR = new Comparator() {
public int compare(Object o1, Object o2) {
if(o1 instanceof Integer)
{
Integer firstNumeric, secondNumeric;
Now, in the getValueAt, make sure that your “int” values are cast into Integer by simply doing:
new Integer(intValue);I hope this will save people some time.