Sorting tables
In the previous examples, the data is arbitrarily displayed. Sorting the data provides more clarity and accuracy in reading and using the information; for example, see the screenshot of the Creating a simple JSF table section. You can try to visually localize the number 1 in the ATP ranking, and number 2 and number 3, and so on, but it is much more useful to have the option of sorting the table by the Ranking column. This is a pretty simple task to implement, especially if you are familiar with Java's List
, Comparator
, and Comparable
features. It is beyond the scope of this book to present these features, but you can accomplish most of the sorting tasks by overriding the compare
method, which has a straightforward flow: it compares both of its arguments for order and returns a negative integer, zero, or a positive integer, as the first argument is less than, equal to, or greater than the second. For example, let's see some common sortings:
Sort the list of strings, such as...