Auto-sorting by columns

ID Name Email Status
87356 Magna At magna-At@dolor.com CLITA
79057 Ea Lorem ea-Lorem@tempor.com DIAM
96366 Dolore Sanctus dolore-sanctus@Stet.com DIAM
89746 Diam Eirmod diam-eirmod@aliquyam.com DOLORE
76479 Amet Justo amet-justo@et.com DOLORES
5834 Elitr Takimata elitr-takimata@ut.com EA
42645 Elitr Sed elitr-sed@voluptua.com ELITR
45723 Sed Et sed-et@et.com INVIDUNT
67325 Erat Dolor erat-dolor@dolore.com KASD
41877 Ipsum Et ipsum-et@diam.com KASD

If you want to allow the user to sort the data as it is coming back, then you need to just do two things, make sure that the data returned from your property implements the Comparable interface (if it doesn't natively - use the decorator pattern as shown a couple of examples ago), and then set the attribute sortable="true" on the columns that you want to be able to sort by.

Note the sortable attribute was previously named sort. The sort attribute is still supported for compatibility with previous versions.

When the user clicks on the column title the rows will be sorted in ascending order and redisplayed on the page. If the user clicks on the column title again, the data will get sorted in descending order and redisplayed.

Only the rows being shown on the page are sorted and resorted, so if you use this attribute along with the pagesize attribute, it will not resort the entire list. It is assumed that if you want to allow the user to resort a large list that will not all fit on a single page, then you will provide an interface which is more appropriate to that task.

When doing sorts, we copy the list into our own internally managed list, so that we don't change the ordering of the original list.

All the parameters received in the first request are preserved in sorting.

You can also define the column to be sorted by default (if the user has not yet clicked on any sortable column) specifying the column index 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.