cancel
Showing results for 
Search instead for 
Did you mean: 

NW 2004 TableSorter with primitive data types

Former Member
0 Kudos

I implemented the TableSorter for NW 2004 as described on SDN. It works quite good with most of my columns. But I have some columns which contain primitive data types (boolean in this case). This causes a ClassCast exception in TableSorter, in this code section:


	  public int compare(Object o1, Object o2)
	  {
	    if (o1 == null && o2 == null)
		  return 0;
		if (o1 == null)
		  return -1;
		if (o2 == null)
		  return +1;
  
		return ((Comparable) o1).compareTo((Comparable) o2);
	  }

Does anybody know, how it is possible to sort data with primitive datatypes?

Thanks in advance!

Martin

Accepted Solutions (1)

Accepted Solutions (1)

venkatakalyan_karanam
Active Contributor
0 Kudos

Hi Martin

private static final Comparator DEFAULT = new Comparator() {

/**

  • Compares the given objects according to the ordering imposed by the first

  • ones <code>compareTo(Object)</code> function. Furthermore, <code>null</code>

  • is treated to be less than any object.

  • @see java.lang.Comparable#compareTo(java.lang.Object)

  • @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

*/

public int compare(Object o1, Object o2) {

if (o1 == null && o2 == null)

return 0;

if (o1 == null)

return -1;

if (o2 == null)

return +1;

if (o1 instanceof Boolean && o2 instanceof Boolean)

return o1.toString().compareTo(o2.toString()); // false < true

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

}

};

May be the above code snippet helps you that to sort the boolean values if at all

present in your table column.

Regards

Kalyan

Former Member
0 Kudos

Thank you very much for your help, it works now!

Cheers,

Martin

Answers (1)

Answers (1)

rajendrengovend
Participant
0 Kudos

Hi Martin,

what you can do is substitute the string value for the boolean value, hence the sort will work fine.

Regards,

Raj