---
title: "SortModel"
---
Defined in: [api/node/typescript/models.ts:604](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L604)

SortModel acts as an adapter model for a given source model by sorting all its
rows according to the order given by `compareFunction`.

### Example

```js
const source = new ArrayModel(["lorem", "ipsum", "dolor"]);
const sorted = new SortModel(source, (a, b) => a.localeCompare(b));

// prints dolor, ipsum, lorem
for (const x of sorted) {
    console.log(x);
}
```

## Extends

- `NativeAdapterModel`\<`T`\>

## Type Parameters

### T

`T`

## Constructors

### Constructor

> **new SortModel**\<`T`\>(`sourceModel`, `compareFunction`): `SortModel`\<`T`\>

Defined in: [api/node/typescript/models.ts:620](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L620)

Constructs a new SortModel that provides a sorted view on the given
`sourceModel` by applying the order given by `compareFunction`.

#### Parameters

##### sourceModel

[`Model`](/latest/docs/node/api/classes/model/)\<`T`\>

the wrapped model.

##### compareFunction

(`a`, `b`) => `number`

compares two rows the same way the callback passed to
                        Array.prototype.sort does.
                        It must not modify `sourceModel` or read this SortModel,
                        and it must define a consistent total order.
                        An inconsistent comparator such as `(a, b) => a > b` makes reading the model fail with an error.

#### Returns

`SortModel`\<`T`\>

#### Overrides

`NativeAdapterModel<T>.constructor`

## Properties

### sourceModel

> `readonly` **sourceModel**: [`Model`](/latest/docs/node/api/classes/model/)\<`T`\>

Defined in: [api/node/typescript/models.ts:608](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L608)

The source model that this SortModel sorts rows from.

## Accessors

### \[nativeModelKey\]

#### Get Signature

> **get** **\[nativeModelKey\]**(): `NativeModel` \| `undefined`

Defined in: [api/node/typescript/models.ts:424](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L424)

##### Returns

`NativeModel` \| `undefined`

#### Inherited from

`NativeAdapterModel.[nativeModelKey]`

## Methods

### \[iterator\]()

> **\[iterator\]**(): `Iterator`\<`T`\>

Defined in: [api/node/typescript/models.ts:195](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L195)

#### Returns

`Iterator`\<`T`\>

#### Inherited from

`NativeAdapterModel.[iterator]`

***

### filter()

> **filter**(`filterFunction`): [`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`T`\>

Defined in: [api/node/typescript/models.ts:118](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L118)

Returns a new Model that only contains the rows of this model for which
`filterFunction` returns true.

#### Parameters

##### filterFunction

(`data`) => `boolean`

returns true if a row should be visible.

#### Returns

[`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`T`\>

a new [FilterModel](/latest/docs/node/api/classes/filtermodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.filter`

***

### insertRow()

> **insertRow**(`_index`, `_data`): `void`

Defined in: [api/node/typescript/models.ts:191](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L191)

Implementations of this function must add a row at the specified index, pushing all next
rows to the right.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to insert.

##### \_data

`T`

new data item to store in a new row.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.insertRow`

***

### map()

> **map**\<`U`\>(`mapFunction`): [`MapModel`](/latest/docs/node/api/classes/mapmodel/)\<`T`, `U`\>

Defined in: [api/node/typescript/models.ts:108](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L108)

Returns a new Model where all elements are mapped by the function `mapFunction`.

#### Type Parameters

##### U

`U`

#### Parameters

##### mapFunction

(`data`) => `U`

maps the data from T to U.

#### Returns

[`MapModel`](/latest/docs/node/api/classes/mapmodel/)\<`T`, `U`\>

a new [MapModel](/latest/docs/node/api/classes/mapmodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.map`

***

### pushRow()

> **pushRow**(`data`): `void`

Defined in: [api/node/typescript/models.ts:169](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L169)

Adds a line to the model with the provided data.
Throws when the model rejects the modification.
The default implementation calls [Model.insertRow](/latest/docs/node/api/classes/model/#insertrow) with the row count.

#### Parameters

##### data

`T`

new data item to store in a new row.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.pushRow`

***

### removeRow()

> **removeRow**(`_index`): `void`

Defined in: [api/node/typescript/models.ts:179](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L179)

Implementations of this function must remove the row at the specified index.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to remove.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.removeRow`

***

### reset()

> **reset**(): `void`

Defined in: [api/node/typescript/models.ts:642](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L642)

Re-applies the sort order on the rows of the source model. Use this if
the compare function depends on state external to the source model and
that state has changed.

#### Returns

`void`

***

### reverse()

> **reverse**(): [`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`T`\>

Defined in: [api/node/typescript/models.ts:137](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L137)

Returns a new Model with the same rows as this model, in reverse order.

#### Returns

[`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`T`\>

a new [ReverseModel](/latest/docs/node/api/classes/reversemodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.reverse`

***

### rowCount()

> **rowCount**(): `number`

Defined in: [api/node/typescript/models.ts:436](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L436)

Returns the number of entries in the model.

#### Returns

`number`

#### Inherited from

`NativeAdapterModel.rowCount`

***

### rowData()

> **rowData**(`row`): `T` \| `undefined`

Defined in: [api/node/typescript/models.ts:445](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L445)

Returns the data at the specified row.

#### Parameters

##### row

`number`

index in range 0..(rowCount() - 1).

#### Returns

`T` \| `undefined`

undefined if row is out of range otherwise the data.

#### Inherited from

`NativeAdapterModel.rowData`

***

### setRowData()

> **setRowData**(`row`, `data`): `void`

Defined in: [api/node/typescript/models.ts:455](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L455)

Stores the given data in the source model at the row that corresponds
to the given row index.

#### Parameters

##### row

`number`

index in range 0..(rowCount() - 1).

##### data

`T`

new data item to store on the given row index.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.setRowData`

***

### sort()

> **sort**(`compareFunction`): `SortModel`\<`T`\>

Defined in: [api/node/typescript/models.ts:129](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L129)

Returns a new Model with the same rows as this model, ordered according to
`compareFunction`.

#### Parameters

##### compareFunction

(`a`, `b`) => `number`

compares two rows the same way the callback passed to
                        Array.prototype.sort does.

#### Returns

`SortModel`\<`T`\>

a new [SortModel](/latest/docs/node/api/classes/sortmodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.sort`

***

### unsortedRow()

> **unsortedRow**(`sortedRow`): `number` \| `undefined`

Defined in: [api/node/typescript/models.ts:652](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L652)

Given a `sortedRow` index into this SortModel, returns the corresponding
row index in the source model.

#### Parameters

##### sortedRow

`number`

index in range 0..(rowCount() - 1).

#### Returns

`number` \| `undefined`

undefined if sortedRow is out of range otherwise the source row index.