hi, i've saw a way to resolve the sorting problem.
here's a sample sorting jsp source ,hope this helps
pleas check it out.
<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<head>
<link href="site.css" rel="stylesheet" type="text/css"/>
<f:loadBundle basename="com.corejsf.messages" var="msgs"/>
<title>
<h
utputText value="#{msgs.windowTitle}"/>
</title>
</head>
<body>
<h:form>
<h
ataTable value="#{tableData.names}" var="name"
styleClass="names" headerClass="namesHeader"
columnClasses="last,first">
<h:column>
<f:facet name="header">
<h:commandLink action="#{tableData.names.sortByLast}">
<h
utputText value="#{msgs.lastColumnHeader}"/>
</h:commandLink>
</f:facet>
<h
utputText value="#{name.last}"/>
<f:verbatim>,</f:verbatim>
</h:column>
<h:column>
<f:facet name="header">
<h:commandLink action="#{tableData.names.sortByFirst}">
<h
utputText value="#{msgs.firstColumnHeader}"/>
</h:commandLink>
</f:facet>
<h
utputText value="#{name.first}"/>
</h:column>
</h
ataTable>
</h:form>
</body>
</f:view>
</html>
package com.corejsf;
import java.util.Arrays;
import java.util.Comparator;
import javax.faces.model.DataModel;
import javax.faces.model.DataModelListener;
public class SortFilterModel extends DataModel {
private DataModel model;
private Row[] rows;
private static Comparator byLast = new
Comparator() {
public int compare(Object o1, Object o2) {
Row r1 = (Row) o1;
Row r2 = (Row) o2;
Name n1 = (Name) r1.getData();
Name n2 = (Name) r2.getData();
return n1.getLast().compareTo(n2.getLast());
}
};
private static Comparator byFirst = new
Comparator() {
public int compare(Object o1, Object o2) {
Row r1 = (Row) o1;
Row r2 = (Row) o2;
Name n1 = (Name) r1.getData();
Name n2 = (Name) r2.getData();
return n1.getFirst().compareTo(n2.getFirst());
}
};
private class Row {
private int row;
public Row(int row) {
this.row = row;
}
public Object getData() {
int originalIndex = model.getRowIndex();
model.setRowIndex(row);
Object thisRowData = model.getRowData();
model.setRowIndex(originalIndex);
return thisRowData;
}
}
public SortFilterModel(DataModel model) {
this.model = model;
int rowCnt = model.getRowCount();
if (rowCnt != -1) {
rows = new Row[rowCnt];
for(int i=0; i < rowCnt; ++i) {
rows[i] = new Row(i);
}
}
}
public String sortByLast() {
Arrays.sort(rows, byLast);
return null;
}
public String sortByFirst() {
Arrays.sort(rows, byFirst);
return null;
}
public void setRowIndex(int rowIndex) {
if (rowIndex == -1 || rowIndex >= model.getRowCount()) {
model.setRowIndex(rowIndex);
}
else {
model.setRowIndex(rows[rowIndex].row);
}
}
// The following methods delegate directly to the
// decorated model
public boolean isRowAvailable() {
return model.isRowAvailable();
}
public int getRowCount() {
return model.getRowCount();
}
public Object getRowData() {
return model.getRowData();
}
public int getRowIndex() {
return model.getRowIndex();
}
public Object getWrappedData() {
return model.getWrappedData();
}
public void setWrappedData(Object data) {
model.setWrappedData(data);
}
public void addDataModelListener(DataModelListener listener) {
model.addDataModelListener(listener);
}
public DataModelListener[] getDataModelListeners() {
return model.getDataModelListeners();
}
public void removeDataModelListener(DataModelListener listener) {
model.removeDataModelListener(listener);
}
}
package com.corejsf;
public class Name {
private String first;
private String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
public void setFirst(String newValue) { first = newValue; }
public String getFirst() { return first; }
public void setLast(String newValue) { last = newValue; }
public String getLast() { return last; }
}
package com.corejsf;
import javax.faces.model.DataModel;
import javax.faces.model.ArrayDataModel;
public class TableData {
private DataModel filterModel = null;
private static final Name[] names = {
new Name("Anna", "Keeney"),
new Name("John", "Wilson"),
new Name("Mariko", "Randor"),
new Name("William", "Dupont"),
};
public TableData() {
ArrayDataModel model = new ArrayDataModel(names);
filterModel = new SortFilterModel(model);
}
public DataModel getNames() {
return filterModel;
}
}
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>tableData</managed-bean-name>
<managed-bean-class>com.corejsf.TableData</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>