Interface

# SortableTableDataSource

<div class="rst-class">

forsearch

</div>

Sortable Table Data Source

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Lets a `TableDataSource</api/interfaces/tabledatasource>` sort its own rows.

Implement it in addition to `TableDataSource</api/interfaces/tabledatasource>` and clicking a sortable column header asks the source to sort instead of sorting inside the table - the table has no copy of the data to sort. After sorting, the table redraws and asks for the cells again.

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                                 | Parameters                                                                                    | Returns                              | Shared |
|------------------------------------------------------|-----------------------------------------------------------------------------------------------|--------------------------------------|--------|
| `SortByColumn<sortabletabledatasource.sortbycolumn>` | column As `Integer</api/data_types/integer>`, ascending As `Boolean</api/data_types/boolean>` | `Integer()</api/data_types/integer>` |        |

## Method descriptions

<div id="sortabletabledatasource.sortbycolumn">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SortableTableDataSource.SortByColumn

**SortByColumn**(column As `Integer</api/data_types/integer>`, ascending As `Boolean</api/data_types/boolean>`) As `Integer()</api/data_types/integer>`

> Reorder the visible projection and return a permutation. The permutation tells the table which row each position came from, so it can carry selection, row heights and tags over to the new order.
>
> This example sorts a list of indices, reorders the records by it and returns it:
>
> ``` xojo
> ' Sort a list of indices, then reorder the records by it
> Var order() As Integer
>
> For i As Integer = 0 To mRows.LastIndex
>   order.Add(i)
> Next
>
> ' A key array sorted together with the indices: the indices follow the keys
> Var keys() As String
>
> For Each i As Integer In order
>   keys.Add(CellTextAt(i, column))
> Next
>
> keys.SortWith(order)
>
> If direction = DesktopTable.SortDirections.Descending Then
>   Var flipped() As Integer
>
>   For i As Integer = order.LastIndex DownTo 0
>     flipped.Add(order(i))
>   Next
>
>   order = flipped
> End If
>
> Var sorted() As Record
>
> For Each i As Integer In order
>   sorted.Add(mRows(i))
> Next
>
> mRows = sorted
>
> ' The permutation lets the table carry selection, row heights and tags over
> Return order
> ```

## Compatibility

|                       |         |
|-----------------------|---------|
| **Project Types**     | Desktop |
| **Operating Systems** | All     |

<div class="seealso">

`DesktopTable</api/user_interface/desktop/desktoptable>`, `TableDataSource</api/interfaces/tabledatasource>`, `EditableTableDataSource</api/interfaces/editabletabledatasource>`, `HierarchicalTableDataSource</api/interfaces/hierarchicaltabledatasource>`

</div>
