---
title: "SortModel[T]"
---
import XRef from "../../../../components/XRef.astro";
import Signature from "../../../../components/Signature.astro";

```python
from slint import SortModel
```

SortModel is a <XRef to="Model" /> that provides the rows of a source model in
sorted order.

Like `sorted()`, it orders the rows by the result of the `key` function,
or by the rows themselves without a key, and in descending order with
`reverse=True`.
To sort by a comparison function, pass `key=functools.cmp_to_key(compare)`.
Rows with a float NaN key sort last, followed by rows whose key raises an
exception, regardless of `reverse`.
Keys that can't be ordered, such as tuples that contain NaN, raise ValueError.

The SortModel follows the changes of the source model.
Setting a row sets the corresponding row of the source model.

```python
names = slint.ListModel(["Max", "Hans", "Roman"])
sorted_names = slint.SortModel(names)
assert list(sorted_names) == ["Hans", "Max", "Roman"]
by_length = slint.SortModel(names, key=len, reverse=True)
```

Alternatively, subclass SortModel and implement `sort_key`.
Call `reset` when the order changes for reasons other than a change of
the source model.

**Bases:** _WritableAdapterModel&#91;T&#93;

## Properties

### source_model

<Signature symbol="slint.models.SortModel.source_model">source_model</Signature>

## Methods

### sort_key

<Signature symbol="slint.models.SortModel.sort_key">sort_key(row_data: T) -&gt; <XRef to="typing.Any" plain /></Signature>

Returns the value to order `row_data`, a row of the source model, by.
The default implementation returns `row_data` itself.
Re-implement this method in a sub-class that doesn't pass a key to the
constructor.

### reset

<Signature symbol="slint.models.SortModel.reset">reset() -&gt; <XRef to="None" plain /></Signature>

Sorts all rows of the source model again.
Call this when the order changes for reasons other than a change of
the source model.

### unsorted_row

<Signature symbol="slint.models.SortModel.unsorted_row">unsorted_row(row: <XRef to="int" plain />) -&gt; <XRef to="int" plain /></Signature>

Returns the index of the row of the source model that is `row` in this model.
Raises IndexError if `row` is out of range.

### notify_row_changed

<Signature symbol="slint.models.Model.notify_row_changed">notify_row_changed(row: <XRef to="int" plain />) -&gt; <XRef to="None" plain /></Signature>

Call this method from a sub-class to notify the views that a row has changed.

### notify_row_removed

<Signature symbol="slint.models.Model.notify_row_removed">notify_row_removed(row: <XRef to="int" plain />, count: <XRef to="int" plain />) -&gt; <XRef to="None" plain /></Signature>

Call this method from a sub-class to notify the views that
`count` rows have been removed starting at `row`.

### notify_row_added

<Signature symbol="slint.models.Model.notify_row_added">notify_row_added(row: <XRef to="int" plain />, count: <XRef to="int" plain />) -&gt; <XRef to="None" plain /></Signature>

Call this method from a sub-class to notify the views that
`count` rows have been added starting at `row`.

### set_row_data

<Signature symbol="slint.models._WritableAdapterModel.set_row_data">set_row_data(row: <XRef to="int" plain />, value: T) -&gt; <XRef to="None" plain /></Signature>

Sets the row of the source model that corresponds to `row`.
Raises IndexError if `row` is out of range.

### row_count

<Signature symbol="slint.models._AdapterModel.row_count">row_count() -&gt; <XRef to="int" plain /></Signature>

### row_data

<Signature symbol="slint.models._AdapterModel.row_data">row_data(row: <XRef to="int" plain />) -&gt; T | None</Signature>

### push_row

<Signature symbol="slint.models.Model.push_row">push_row(value: T) -&gt; <XRef to="None" plain /></Signature>

Add a new row to the model with the provided value.
The default implementation calls `insert_row` with the row count.

### remove_row

<Signature symbol="slint.models.Model.remove_row">remove_row(row: <XRef to="int" plain />) -&gt; <XRef to="None" plain /></Signature>

Remove the row at the given index.
Raises an exception when the model rejects the modification.
The default implementation raises NotImplementedError. A model that
supports removing rows should also call `notify_row_removed`.

### insert_row

<Signature symbol="slint.models.Model.insert_row">insert_row(row: <XRef to="int" plain />, value: T) -&gt; <XRef to="None" plain /></Signature>

Insert a new row at the given index.
Raises an exception when the model rejects the modification.
The default implementation raises NotImplementedError. A model that
supports inserting rows should also call `notify_row_added`.