|
Re: ViewerSorter problem with a Table [message #466081 is a reply to message #466066] |
Fri, 30 December 2005 22:00 |
Luis Reyes Messages: 14 Registered: July 2009 |
Junior Member |
|
|
I'm going to assume that your criteria is the same as the column header.
So let's say your object are player objects and there's 4 columns in your
table: name, last name, points, rebounds and assists. in your sorter
class, don't override the sort method, override the compare method. Here
is an example:
//assume that in the constructor you passed the index number of the
//column that you want to sort, in a variable called column
public int compare(Viewer viewer, Object e1, Object e2) {
int returnValue = 0;
if (e1 instanceof Player) {
Player p1 = (Player) e1;
Player p2 = (Player) e2;
// Determine which column and do the appropriate sort
switch (column) {
case PlayerConst.COLUMN_FIRST_NAME:
returnValue = collator.compare(p1.getFirstName(), p2.getFirstName());
break;
case PlayerConst.COLUMN_LAST_NAME:
returnValue = collator.compare(p1.getLastName(),
p2.getLastName());
break;
case PlayerConst.COLUMN_POINTS:
returnValue = p1.getPoints() > p2.getPoints() ? 1 : -1;
break;
case PlayerConst.COLUMN_REBOUNDS:
returnValue = p1.getRebounds() > p2.getRebounds() ? 1 : -1;
break;
case PlayerConst.COLUMN_ASSISTS:
returnValue = p1.getAssists() > p2.getAssists() ? 1 : -1;
break;
}
// If descending order, flip the direction
// introduce some kind of if statement and
// check sort direction, then you can say
// returnValue = -returnValue;
return returnValue;
}
}
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03678 seconds