Sorter
public abstract class Sorter {
private boolean ascending = true;
private final int column;
public Sorter() {
this(0, true);
}
public Sorter(int col, boolean ascending) {
column=col;
setAscending(ascending);
}
public int compare(int row1, int row2) {
int result = compare(row1, row2, getColumnIndex());
return ascending ? result : -result;
}
private int compare(int row1, int row2, int col) {
Object o1 = getInputValue(row1, col);
Object o2 = getInputValue(row2, col);
if (o1 == null && o2 == null) {
return 0;
}
else if (o1 == null) {
return -1;
}
else if (o2 == null) {
return 1;
}
try
{
Double.parseDouble(o1.toString());
Double.parseDouble(o2.toString());
o1= new Double(o1.toString());
o2= new Double(o2.toString());
}
catch(Exception e)
{
}
if (o1 instanceof Comparable) {
if(o1 instanceof Double || o2 instanceof Double)
{
double dbl1= Double.parseDouble(o1.toString());
double dbl2= Double.parseDouble(o2.toString());
if (dbl1 < dbl2) {
return -1;
} else if (dbl1 > dbl2) {
return 1;
} else {
return 0;
}
}
Comparable c1 = (Comparable) o1;
Comparable c2 = (Comparable) o2;
return c1.compareTo(c2);
}
else if (o1 instanceof Boolean) {
try {
Boolean bool1 = (Boolean) o1;
boolean b1 = bool1.booleanValue();
Boolean bool2 = (Boolean) o2;
boolean b2 = bool2.booleanValue();
if (b1 == b2) {
return 0;
}
else if (b1) { // Define false < true
return 1;
}
else {
return -1;
}
}
catch (ClassCastException ex) {
System.out.println("Column class mismatch: " + o1.getClass() +
" can't be compared to " + o2.getClass());
}
}
else {
return o1.toString().compareTo(o2.toString());
}
return 0;
}
public boolean isAscending() {
return ascending;
}
public void setAscending(boolean ascending) {
this.ascending = ascending;
refresh();
}
public void toggle() {
ascending = !ascending;
}
public int getColumnIndex() {
return column;
}
}