Previous Page
Next Page

5.1.3. Viewer sorters

A viewer sorter (see Figure 5-5 for the ViewerSorter hierarchy) is used to sort the elements provided by the content provider (see Figure 5-1). If a viewer does not have a viewer sorter, the elements are shown in the order returned by the content provider. A viewer sorter is associated with a viewer using the setSorter() method.

Figure 5-5. ViewerSorter hierarchy.


The default sorting algorithm uses a two-step process. First, it groups elements into categories (ranked 0 through n); and second, it sorts each category based on the text labels returned by the label provider. By default, all items are in the same category, so all the items are sorted relative to their text labels. Your application can override the default categorization as well as the default comparison routine to use some criteria other than the item's text label.

Useful APIs defined by ViewerSorter include:

category(Object) Returns the category of the given element.

compare(Viewer, Object, Object) Returns a negative, zero, or positive number depending on whether the first element is less than, equal to, or greater than the second element.

getCollator() Returns the collator used to sort strings.

isSorterProperty(Object, String) Returns whether this viewer sorter would be affected by a change to the given property of the given element.

sort(Viewer viewer, Object[]) Sorts the given elements in place, modifying the given array.

For an example of viewer sorters, see Section 5.1.6, ListViewer class, on page 192.


Previous Page
Next Page