TableSorter, how to sort differently depending of order('a-z' vs. 'z-a')

publicstaticfinal Comparator COMPARABLE_COMPARATOR =new Comparator(){

publicint compare(Object o1, Object o2){

if (("".equals(o1) ||"".equals(o2))return ((Comparable) o2).compareTo(o1);

return ((Comparable) o1).compareTo(o2);

}

};

THE OBJECT IS TO GET FIELDS THAT EQUALS("") TO ALWAYS BE SORTED LAST INDEPENDENT OF ORDER.

So for so good, when sorting A-Z it puts the blank in the end,

and as previously stated i want it to put ("") in the end regardless of order.

Ive been trying for quite some time to get this working, but i cant get it right.. Does anyone know, and can tell me how to get make it work..

My guess, is that i need to add to the if statement..

if ("".equals(o1) ||"".equals(o2))

And somehow check if sorting method is reverse (ie. from z-a),

(lets pretend we have a boolean reverse; that keeps track of this.

then i guess the if statement should look like..

if ("".equals(o1) ||"".equals(o2)) && (!reverse)){ return...}

But as i said, i have yet failed to get this type of sorting to work.

I should also declare that i have no such boolean keeping track of sorting either, nor have i been able to implement such a thing.

So i would be most greatful for any help solving this matter!

Thanks in advance!

http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorter.java - TableSorter class in full.

http://specview.stsci.edu/doc/spv/util/TableSorter.html - Doc..

[2223 byte] By [raindrop__a] at [2007-11-26 17:42:51]
# 1

Hi Raindrop,

Do I get it right: You want to use the TableSorter class from the example (your link) but make it out empty values always at the end?

The TableSorter example has an internal helper class Row which implements Comparable. I t explicitely puts null values at the beginning. Look for "// Define null less than everything, except null." You could just put your code there:

if (comparison != 0) {

// line above is last from example code, new code below

// Define "" bigger than everything else

if ("".equals(o1) && "".equals(o2) ) {

comparison = 0;

} else if ("".equals(o1) {

comparison = 1;

} else if ("".equals(o2)) {

comparison = -1;

} else {

comparison = getComparator(column).compare(o1, o2);

// end of new code

}

}

Is that what you want to accomplish?

... Michael

michaelhhha at 2007-7-9 0:11:03 > top of Java-index,Desktop,Core GUI APIs...
# 2
Do you want to write your own comparator for every column, or change the TableSorter code?
Rodney_McKaya at 2007-7-9 0:11:03 > top of Java-index,Desktop,Core GUI APIs...
# 3
try using TreeSet.
mutant2000a at 2007-7-9 0:11:03 > top of Java-index,Desktop,Core GUI APIs...