2
votes

I am unable to sort the date in primefaces datatable.

 <p:dataTable id="tabl" var="stu" value="#{student.fetchData()}"
 sortBy="#{stu.dateOfBirth}" sortOrder="ascending" >

it is sorting by date but order is not correct. it sorting like strings but not dates. how can I sort by date in dataTable?

2
Are you want to sort the whole table or just one column by date ? - Scorpion
whole table. I want default sort by dateofbirth. - stackUser44
What is the type of dateOfBirth in the backing bean? - Yannick
That's where you implemented the sorting function. The table itself does not do anything. It just passes the filter value and the sort order that you need to use in the sorting function so that it can sort the column by dates. - Tiny
dateofbirth type is date - stackUser44

2 Answers

2
votes

try to sort by dateOfBirth.time

<p:dataTable id="tabl" var="stu" value="#{student.fetchData()}"
 sortBy="#{stu.dateOfBirth.time}" sortOrder="ascending" >
1
votes

The trick is that you need to make the list of Date type i.e. List<Date> (Please note that if your List is of String type, it will not work. And 2nd important step is to use <f:convertDateTime pattern="dd.MM.yyyy" />

<h:outputText value="#{(<From the backing you are getting the date here>)}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>

In your case it should be like List<Date> dateOfBirth instead of List<String>. So in the form backing bean if you print the logs, even you get Date in format E MMM dd HH:mm:ss Z yyyy for example Tue May 21 10:47:34 IST 2019 thats ok and you need not to worry as this date will be converted to corresponding date format which you will convert by using <f:convertDateTime> in your xhtml file.

I tried this and it worked for me. Incase you have different solution please feel free to share :) Detailed explanation : dateFormatter