cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Functionality to dynamically sort tableview columns

Former Member
0 Likes
963

Has anybody successfully tried sorting columns of a tableview component by clicking on its header? I need to sort a column containing dates in mm/dd/yyyy format. Please share your hints or code. BR, Maulin

View Entire Topic
Former Member
0 Likes

Hello,

I have created a new TableModel that extends the DefaultTableViewModel. I has this functionality as a standard....

Let me know if you want the code so i can send it to you..

BR

Max

Former Member
0 Likes

Hi Max,

I am very interested in your code, it would be really great if you could post it here, or send it via E-Mail.

Kind regards

Francisco

Former Member
0 Likes

Hello All,

First of all thanks a lot for sharing interest & your experience here.

Max, Could you please send DefaultTableViewModel code to me on maulindoshi@hotmail.com ?

BR,

Maulin

Message was edited by: Maulin Doshi

Former Member
0 Likes

Hi Fransisco,

Send your e-mail so that i can send the code to you.

BR

Max

Former Member
0 Likes

Hi Max,

i have the same request , could u please senf me yr code?(my mail juranj1@fel.cvut.cz)

but i thing that a lot of users is interested in yr code so i thing that it will be better to post here.

thanks

JJ

Former Member
0 Likes

Hi,

Here is the code, no support included :-):

import java.util.Collections;

import java.util.Comparator;

import java.util.Vector;

import com.sapportals.htmlb.table.DefaultTableViewModel;

public class SortTableModel extends DefaultTableViewModel implements Comparator {

protected int currCol;

protected Vector ascendCol; // this vector stores the state (ascending or descending) of each column

protected Integer one = new Integer(1);

protected Integer minusOne = new Integer(-1);

public SortTableModel() {

super();

ascendCol = new Vector();

}

public SortTableModel(Vector vec) {

super(vec);

ascendCol = new Vector();

}

public SortTableModel(Vector vec, Vector vec2, int numberOfColumns) {

super(vec, vec2);

ascendCol = new Vector();

setSortOrder(numberOfColumns);

}

/*****************************************************************

  • This method is the implementation of the Comparator interface.

  • It is used for sorting the rows

*****************************************************************/

public int compare(Object v1, Object v2) {

// the comparison is between 2 vectors, each representing a row

// the comparison is done between 2 objects from the different rows that are in the column that is being sorted

int ascending = ((Integer)ascendCol.get(currCol)).intValue();

if (v1 == null && v2 == null) {

return 0;

} else if (v2 == null) { // Define null less than everything.

return 1 * ascending;

} else if (v1 == null) {

return -1 * ascending;

}

Object o1 = ((Vector)v1).get(currCol);

Object o2 = ((Vector)v2).get(currCol);

// If both values are null, return 0.

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

return 0;

} else if (o2 == null) { // Define null less than everything.

return 1 * ascending;

} else if (o1 == null) {

return -1 * ascending;

}

if (o1 instanceof Number && o2 instanceof Number) {

Number n1 = (Number)o1;

double d1 = n1.doubleValue();

Number n2 = (Number)o2;

double d2 = n2.doubleValue();

if (d1 == d2) {

return 0;

} else if (d1 > d2) {

return 1 * ascending;

} else {

return -1 * ascending;

}

} else if (o1 instanceof Boolean && o2 instanceof Boolean) {

Boolean bool1 = (Boolean)o1;

boolean b1 = bool1.booleanValue();

Boolean bool2 = (Boolean)o2;

boolean b2 = bool2.booleanValue();

if (b1 == b2) {

return 0;

} else if (b1) {

return 1 * ascending;

} else {

return -1 * ascending;

}

} else {

// default case

if (o1 instanceof Comparable && o2 instanceof Comparable) {

Comparable c1 = (Comparable)o1;

Comparable c2 = (Comparable)o2; // superflous cast, no need for it!

try {

return c1.compareTo(c2) * ascending;

} catch (ClassCastException cce) {

// forget it... we'll deal with them like 2 normal objects below.

}

}

String s1 = o1.toString();

String s2 = o2.toString();

return s1.compareTo(s2) * ascending;

}

}

/***************************************************************************

  • This method sorts the rows using Java's Collections class.

  • After sorting, it changes the state of the column -

  • if the column was ascending, its new state is descending, and vice versa.

***************************************************************************/

public void sort() {

Collections.sort(dataVector, this);

Integer val = (Integer)ascendCol.get(currCol);

ascendCol.remove(currCol);

if (val.equals(one)) // change the state of the column

ascendCol.add(currCol, minusOne);

else

ascendCol.add(currCol, one);

}

public void sortByColumn(int column) {

this.currCol = column;

sort();

}

public void setSortOrder(int numberOfColumns) {

for (int i = 0; i < numberOfColumns; i++) {

ascendCol.add(one);

}

}

}