An easy way to correctly sort date fields in Java displaytag
The problem: Display tag sorts all values as Strings which means that date values are sorted incorrectly by displaytag. One way to do this is write your own decorator class which implements a sort for dates and use this decorator on your field.
A quick and easy way to sort date items correctly without having to write your own decorator class is to: change the date format to yyyy/mm/dd This is a date display which correctly sorts in string representation. It is that easy.
If you want sort your list by any colum by default you can specify the column index of the column you want to sort in the defaultsort="" attribute of the table tag. The default order can be set using the defaultorder attribute: the default value is ascending, you can set it to descending to reverse the order. So, to sort by default your date column, set defaultsort equal to the column index of the date column (remember 0 based indexing) and then set defaultorder="decending" so you have most recent dates first.
A quick and easy way to sort date items correctly without having to write your own decorator class is to: change the date format to yyyy/mm/dd This is a date display which correctly sorts in string representation. It is that easy.
If you want sort your list by any colum by default you can specify the column index of the column you want to sort in the defaultsort="" attribute of the table tag. The default order can be set using the defaultorder attribute: the default value is ascending, you can set it to descending to reverse the order. So, to sort by default your date column, set defaultsort equal to the column index of the date column (remember 0 based indexing) and then set defaultorder="decending" so you have most recent dates first.

1 Comments:
display:column sortable="true" title="Some Sortable Date" >
< span style="display:none">< c:out value="${item.myDate.time}"/ >< /span>< fmt:formatDate value="${item.myDate}" pattern="dd-MMM-yyyy" / >< /display:column
also works without having the yyyy-MM-dd restriction. The crucial hack as bolded.
Excuse the malformed HTML snippet, I had to hack it to upload.
Post a Comment
<< Home