I am trying to sort my custom created SortableDataModel but it doesn't work. There is a commandLink which when clicked calls sort method in backing bean which sorts the custom SortableDataModel which i created. This correctly worked for other xhtml page but this page does not call the method properly i guess. I am not sure if it's an issue with method not being called or issue with the logic in my method.
private String sortSelection;
//.... getters and setters..
//Sort Method
public String sort(){
try{
if(asc == true) {
if (this.sortSelection.equalsIgnoreCase("id")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return Integer.compare(o1.getStateId(), o2.getStateId());
}
});
}
if (sortSelection.equalsIgnoreCase("states")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o1.getState().getStateName().compareTo(o2.getState().getStateName());
}
});
}
if (sortSelection.equalsIgnoreCase("population")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o1.getState().getStatePopulation().compareTo(o2.getState().getStatePopulation());
}
});
}
if (sortSelection.equalsIgnoreCase("area")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o1.getState().getStateArea().compareTo(o2.getState().getStateArea());
}
});
}
asc = false;
}
else{
if (sortSelection.equalsIgnoreCase("id")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return Integer.compare(o2.getStateId(), o1.getStateId());
}
});
}
if (sortSelection.equalsIgnoreCase("states")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o2.getState().getStateName().compareTo(o1.getState().getStateName());
}
});
}
if (sortSelection.equalsIgnoreCase("population")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o2.getState().getStatePopulation().compareTo(o1.getState().getStatePopulation());
}
});
}
if (sortSelection.equalsIgnoreCase("area")) {
stateModel.sortBy(new Comparator<India>() {
@Override
public int compare(India o1, India o2) {
return o2.getState().getStateArea().compareTo(o1.getState().getStateArea());
}
});
}
asc = true;
}
return null;
}
catch (Exception e) {
return null;
}
}
My XHTML FILE
<h:dataTable value="#{countryDetails.stateModel}" var="india"
styleClass="country-table"
headerClass="country-table-header"
rowClasses="country-table-odd-row,country-table-even-row"
border="1">
<h:column>
<f:facet name="header">
<h:outputLabel value="Id"/>
<br/>
<h:commandLink action="#{countryDetails.sort}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="id" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.stateId}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="States"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="states" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.state.stateName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="State Capitals"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="capitals" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.state.stateCapital}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Population"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="population" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.state.formattedPop}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Area (in sq. kms.)"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="area" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.state.formattedArea}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Language"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="language" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.state.stateLanguage}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Government Type"/>
</f:facet>
<h:outputLabel value="#{india.govtStructure.govtType}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Total Candidacies"/>
<br/>
<h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
<f:setPropertyActionListener value="candidacies" target="#{countryDetails.sortSelection}" />
</h:commandLink>
</f:facet>
<h:outputLabel value="#{india.govtStructure.totalParties}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Current Ruling Party"/>
</f:facet>
<h:outputLabel value="#{india.govtStructure.currentRulingParty}" />
</h:column>
I guess commandLink is not calling the method correctly because nothing in the sort() method is executed. I am stuck. Help needed