How to order a P: dataTable

How do I sort a datatable by a text. For example my system is a service order system, I need "open" orders to come first. Follow the datatable code below. I tried using sortby more did not give me result.

This is my listall:

public List<Ordens> listarTodos() { 
String sql = "select o from Ordens as o inner join o.usuario as u where u.id " 
           + " = "+UserStatic.getUsuario().getId(); 

I need to sort by this column:

<p:column filterBy="#{b.situacao}" headerText="Situação Técnico" sortBy="#{b.situacao}" style="width: 98px;"> <h:outputText value="#{b.situacao}" style=" color: red"/> </p:column> 

The 'Open' situation has to come first.

Maybe ordering in sql would work, more do not know how do in sql, if anyone knows. I tried so more did not work

    public List<Ordens> listarTodos() {
    String sql = "select o from Ordens as o inner join o.usuario as u where      u.id"
            + " = "+UserStatic.getUsuario().getId()
            +"order by o.situacao DESC";
Author: Leandro Santos, 2016-04-14

2 answers

Complementing @Rafael's answer, you can also use the sortBy tag inside the dataTable for the data to appear sorted to the user without the need to click on the column to sort.

    <p:dataTable sortBy="{ordem.status}"
                 var="ordem">
            <p:column headerText="Status" sortBy="#{ordem.status}">
                  <h:outputText value="#{ordem.status}" />
            </p:column>
    </p:dataTable>
 2
Author: Aline Rocha, 2016-04-25 22:01:14

If you want to show your already sorted table you have to sort your list of Table objects before showing it.

Or

Add the sortBy in the table column:

<p:column headerText="Status" sortBy="#{ordem.status}">
      <h:outputText value="#{ordem.status}" />
</p:column>
 1
Author: Rafael, 2016-04-14 12:15:19