user1423793 user1423793 - 6 months ago 46
Java Question

Sorting h:dataTable

I am using Seam and Java to make an h:dataTable that contains information from a ArrayList filled with objects. My question is, is there any way to sort the table by clicking on one of the headers. I have some code right now and all it does is reload the table the same way that it was.

Here's some of my code. Let me know if you need more:

<h:dataTable value="#{deliveryPort.getWholeDeliveryList()}" var="delivery" rules="rows">
<div class="setWidth">
<div class="white">
<f:facet name="header">
<h:outputLink style="color:#FFFFFF" action="#{deliveryPort.sortByAddress()}">
<h:outputText value="#{delivery.address}" />


public String sortByAddress(){
for(int i = 0; i < wholeDeliveryList.size(); i++){
for(int j= i+1; j<wholeDeliveryList.size(); j++){
FlowerStoreDelivery temp = wholeDeliveryList.get(i);
wholeDeliveryList.set(i, wholeDeliveryList.get(j));
wholeDeliveryList.set(j, temp);
return "deliveryList.seam";


public List<FlowerStoreDelivery> getWholeDeliveryList(){
wholeDeliveryList = new ArrayList<FlowerStoreDelivery>();
wholeDeliveryList.addAll(entityManager.createQuery("SELECT e FROM FlowerStoreDelivery e").getResultList());
return wholeDeliveryList;


You can obtain a lot more info about DataTable including sorting and much more on this blog by BalusC, a JSF expert.