'Why Prime Faces Data Table sorting not working

I am new to prime faces and want to sort a data table using sort by and attributes but the table is not being sorted. I am using 5.3 PF.

<p:dataTable id="myTable"
             styleClass="standardTable marginTopFive paddingBottom"
             value="#{User.users}" paginator="true"
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
             paginatorPosition="bottom" rows="10" var="user"
             emptyMessage="Error"
             sortBy="#{user.userId}" sortOrder="descending">
  <f:facet name="spacer">
    <f:verbatim> &nbsp;
    </f:verbatim>
  </f:facet>

  <f:facet name="footer">
  </f:facet>

  <p:column width="8%">
    <f:facet name="header">
    </f:facet>
    <h:column>
      <h:selectBooleanCheckbox
        value="#{user.checked[user.userId]}" />
    </h:column>
  </p:column>
  <p:column width="20%" sortBy="#{mpc.name}">
    <f:facet name="header">
      <h:outputText
        value="User Name" />
    </f:facet>
    <h:outputText value="#{user.user_name}" />
  </p:column>
  <p:column width="30%" sortBy="#{mpc.email}">
    <f:facet name="header">
      <h:outputText
        value="Email" />
    </f:facet>
    <h:outputText value="#{user.user_email}" />
  </p:column>
</p:dataTable>


Solution 1:[1]

You set the name of the iterator variable as var="user", but here <p:column sortBy="#{mpc.name}"/> you called it as mpc.

So you must replace <p:column sortBy="#{mpc.name}"/> with <p:column sortBy="#{user.user_name}"/> and <p:column sortBy="#{mpc.email}"> with <p:column sortBy="#{user.user_email}">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stefan-dan