title: PrimeFaces DataTable rendering list wrong after listener sort (bug?)
body: <p><code>DataTable</code> does refresh, but not rendering list correctly. (JSF 2.2, Mojarra 2.2.0, PrimeFaces 5.2)</p>
  
  <p>I have a list of dates in a PrimeFaces <code>DataTable</code> that I'm reordering each time the user adds or edits the date. This almost works perfectly. Please help me see what I'm doing wrong, or let me know I've found a bug. (Note, its not the <code>update</code> target, since I tried using <code>update="@all"</code> which didn't change anything.)</p>
  
  <p>Try this with my code below:</p>
  
  <p>1) Add 11/21/2015 then add 11/20/2015. Notice it reordered them <code>onAdd</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/TjpmK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TjpmK.png" alt="enter image description here"></a></p>
  
  <p>Console:</p>
  
  <pre><code>onAdd, after
  DataTableEntry[Date:Fri Nov 20 00:00:00 EST 2015]
  DataTableEntry[Date:Sat Nov 21 00:00:00 EST 2015]
  </code></pre>
  
  <p>2) Then edit 11/21/2015 changing it to 11/19/2015. Notice the edited one went away and now there's two 20's. <strong>Bug?!?</strong></p>
  
  <p><a href="https://i.stack.imgur.com/gwbiW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gwbiW.png" alt="enter image description here"></a></p>
  
  <p>Console:</p>
  
  <pre><code>onRowEdit, before
  DataTableEntry[Date:Fri Nov 20 00:00:00 EST 2015]
  DataTableEntry[Date:Thu Nov 19 00:00:00 EST 2015]
  onRowEdit, after
  DataTableEntry[Date:Thu Nov 19 00:00:00 EST 2015]
  DataTableEntry[Date:Fri Nov 20 00:00:00 EST 2015]
  </code></pre>
  
  <p>3) Then hit my "Refresh page" commandButton. It now displays correctly.</p>
  
  <p><a href="https://i.stack.imgur.com/Xk6Dp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xk6Dp.png" alt="enter image description here"></a></p>
  
  <p>Adding dates never has a problem regardless of its before/between/after positions. I found that you can edit the date, and as long as it doesn't actually change the order, it works fine. The moment the order is changed, the edited one disappears and one next to it gets visually duplicated into its place. </p>
  
  <p>datatablesort.xhtml</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      &gt;
  &lt;h:head&gt;
      &lt;title&gt;&lt;ui:insert name="title"&gt;DataTable&lt;/ui:insert&gt;&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
      &lt;h:form id="modelerForm"&gt;
          &lt;p:commandButton value="Refresh page" ajax="false"/&gt;
  
          &lt;h:outputLabel for="date" value="Date"/&gt;
          &lt;h:panelGroup&gt;
              &lt;p:calendar id="date" pattern="MM/dd/yyyy" placeholder="mm/dd/yyyy" mask="true" showOn="button" navigator="true"/&gt;
              &lt;h:commandButton value="Add"&gt;
                  &lt;f:ajax event="action" listener="#{dataTableBean.onAdd}" execute="date" render="entries"/&gt;
              &lt;/h:commandButton&gt;
          &lt;/h:panelGroup&gt;
  
          &lt;p:dataTable id="entries" value="#{dataTableBean.entries}" var="entry" emptyMessage="No dates added" editable="true" tableStyle="width:auto"&gt;
              &lt;p:ajax event="rowEdit" listener="#{dataTableBean.onRowEdit}" update="entries"/&gt;
              &lt;p:column headerText="Dates"&gt;
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{entry.date}"&gt;
                              &lt;f:convertDateTime pattern="MM/dd/yyyy" /&gt;
                          &lt;/h:outputText&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:calendar id="date" value="#{entry.date}" pattern="MM/dd/yyyy" placeholder="mm/dd/yyyy" mask="true" showOn="button" navigator="true"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
              &lt;p:column&gt;
                  &lt;p:rowEditor/&gt;
              &lt;/p:column&gt;
              &lt;p:column&gt;
                  &lt;p:commandLink title="Remove date"&gt;
                      &lt;h:outputText value="" styleClass="ui-icon ui-icon-trash"/&gt;
                      &lt;f:ajax event="click" listener="#{dataTableBean.entries.remove(entry)}" render="entries"/&gt;
                  &lt;/p:commandLink&gt;
              &lt;/p:column&gt;
          &lt;/p:dataTable&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>DataTableBean.java</p>
  
  <pre><code>package com.aadhoc.test;
  
  import java.util.ArrayList;
  import java.util.Comparator;
  import java.util.Date;
  import java.util.List;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  import javax.faces.component.UIComponent;
  import javax.faces.component.UIInput;
  import javax.faces.event.AjaxBehaviorEvent;
  
  import org.primefaces.component.datatable.DataTable;
  import org.primefaces.event.RowEditEvent;
  
  @ManagedBean(name="dataTableBean")
  @SessionScoped
  public class DataTableBean {
  
      private List&lt;DataTableEntry&gt; entries = new ArrayList&lt;&gt;();
      public List&lt;DataTableEntry&gt; getEntries() {
          return entries;
      }
      public void setEntries(List&lt;DataTableEntry&gt; entries) {
          this.entries = entries;
      }   
  
      public void onAdd(AjaxBehaviorEvent event) {
          DumpList("onAdd, before");
  
          UIComponent component = event.getComponent();
          UIComponent dateComp = component.findComponent("date");
          Date date = (Date) ((UIInput)dateComp).getValue();
          DataTableEntry entry = new DataTableEntry();
          entry.setDate(date);
          getEntries().add(entry);
  
          sortEntries(getEntries());
          DumpList("onAdd, after");
      }
  
      public void onRowEdit(RowEditEvent event) {
          DumpList("onRowEdit, before");
  
          // Get entries via DataTable#getValue because in real code
          // I don't have direct access to entries.
          DataTable dt = (DataTable) event.getSource();
          @SuppressWarnings("unchecked")
          List&lt;DataTableEntry&gt; entries = (List&lt;DataTableEntry&gt;) dt.getValue();
          sortEntries(entries);
  
          DumpList("onRowEdit, after");
      }
  
      private void DumpList(String msg) {
          System.out.println(msg);
          for (DataTableEntry entry : entries) {
              System.out.println(entry);
          }
      }
  
      private &lt;T&gt; void sortEntries(List&lt;DataTableEntry&gt; entries) {
          entries.sort(new Comparator&lt;DataTableEntry&gt;() {
              @Override
              public int compare(DataTableEntry entry1, DataTableEntry entry2) {
                  return entry1.getDate().compareTo(entry2.getDate());
              }
          });
      }
  }
  </code></pre>
  
  <p>DataTableEntry.java</p>
  
  <pre><code>package com.aadhoc.test;
  
  import java.io.Serializable;
  import java.util.Date;
  
  public class DataTableEntry implements Serializable {
      private static final long serialVersionUID = -2513940455250513641L;
      private Date date;
      public Date getDate() {
          return date;
      }
      public void setDate(Date date) {
          this.date = date;
      }
  
      public String toString() {
          return getClass().getSimpleName()+"[Date:"+getDate()+"]";
      }
  }
  </code></pre>