Skip to content

2. Sorting

Evren Coşkun edited this page Jan 15, 2020 · 1 revision

TableView has a sorting functionality with 0.8.5.1 version. TableView does not store or copy the data in its TableModel; instead, it maintains a map from the row indexes of the view to the row indexes of the model.

1. ISortableModel to your Cell Model

To be able to use this feature on your TableView. You have to implement ISortableModel to your Cell Model. This interface wants two methods from your cell model. These are:

  • To compare sorted items ordered by normal items in terms of "are items the same":
    String getId()
  • To compare sorted items ordered by normal items in terms of "are contents the same":
    Object getContent()

As you have seen getContent value returns Object. TableView controls the type of the object. And It sorts by considering to the type of class. So you can sort any type of value. Such as;

  • Number
  • String
  • Date
  • Boolean
  • Comparable

2. AbstractSorterViewHolder to your Column Header ViewHolder

AbstractSorterViewHolder helps to listen to change of sorting actions. So you can do whatever you want on any sorting state.

  • This interface method will be called after each sorting process. Note : It will be also called every recycling process.
    onSortingStatusChanged(SortState sortState)
  • This method gives current Sorting state:
    SortState getSortState()

Sorting States

    /**
     * Enumeration value indicating the items are sorted in increasing order.
     * For example, the set <code>1, 4, 0</code> sorted in
     * <code>ASCENDING</code> order is <code>0, 1, 4</code>.
     */
     ASCENDING,
    
    /**
     * Enumeration value indicating the items are sorted in decreasing order.
     * For example, the set <code>1, 4, 0</code> sorted in
     * <code>DESCENDING</code> order is <code>4, 1, 0</code>.
     */
    DESCENDING,
    
    /**
     * Enumeration value indicating the items are unordered.
     * For example, the set <code>1, 4, 0</code> in
     * <code>UNSORTED</code> order is <code>1, 4, 0</code>.
     */
    UNSORTED

3. Helper methods for the sorting process

Several helper methods have been inserted on TableView. These are:

  • To sort the TableView according to a specified column:
    sortColumn(int column, SortState sortState)
  • To get the current sorting state of a column:
    SortState getSortingStatus(int column)