#

API Docs for: 3.10.3
Show: Inherited Protected Private Deprecated

# Tree.Sortable Class

Defined in: `tree/js/extensions/tree-sortable.js:11`
Module: tree-sortable
Parent Module: tree

Extension for `Tree` that makes nodes sortable.

## Constructor

### `Tree.Sortable`

(

- `[config]`

)

Defined in `tree/js/extensions/tree-sortable.js:11`

#### Parameters:

- `[config]` Object optional

  Configuration options.

  - `[sortComparator]` Function optional

    Default comparator function to use when sorting a node's children if the node itself doesn't have a custom comparator function. If not specified, insertion order will be used by default.

  - `[sortReverse=false]` Boolean optional

    If `true`, node children will be sorted in reverse (descending) order by default. Otherwise they'll be sorted in ascending order.

- Index
- Methods
- Properties
- Events

## Item Index

### Methods

- _compare
- _compareReverse
- _getDefaultNodeIndex
- _sort
- sortComparator
- sortNode

### Properties

- sortReverse

### Events

- sort

## Methods

### `_compare`

(

- `a`
- `b`

)
Number protected

Defined in `tree/js/extensions/tree-sortable.js:145`

Compares value a to value b for sorting purposes.

Values are assumed to be the result of calling a sortComparator function.

#### Parameters:

- `a` Mixed

  First value to compare.

- `b` Mixed

  Second value to compare.

#### Returns:

Number: `-1` if a should come before b, `0` if they're equivalent, `1` if a should come after b.

### `_compareReverse`

(

- `a`
- `b`

)
Number protected

Defined in `tree/js/extensions/tree-sortable.js:161`

Compares value a to value b for sorting purposes, but sorts them in reverse (descending) order.

Values are assumed to be the result of calling a sortComparator function.

#### Parameters:

- `a` Mixed

  First value to compare.

- `b` Mixed

  Second value to compare.

#### Returns:

Number: `-1` if a should come before b, `0` if they're equivalent, `1` if a should come after b.

### `_getDefaultNodeIndex`

(

- `parent`
- `node`
- `[options]`

)
Number protected

Defined in `tree/js/extensions/tree-sortable.js:178`

Overrides `Tree#_getDefaultNodeIndex()` to provide insertion-time sorting for nodes inserted without an explicit index.

#### Parameters:

- `parent` Tree.Node

  Parent node.

- `node` Tree.Node

  Node being inserted.

- `[options]` Object optional

  Options passed to `insertNode()`.

#### Returns:

Number: Index at which node should be inserted into parent's `children` array.

### `_sort`

(

- `a`
- `b`
- `comparator`
- `[reverse=false]`

)
Number protected

Defined in `tree/js/extensions/tree-sortable.js:232`

Array sort function used by `sortNode()` to re-sort a node's children.

#### Parameters:

- `a` Tree.Node

  First node to compare.

- `b` Tree.Node

  Second node to compare.

- `comparator` Function

  Comparator function.

- `[reverse=false]` Boolean optional

  If `true`, this will be a reverse (descending) comparison.

#### Returns:

Number: `-1` if a is less than b, `0` if equal, `1` if greater.

### `sortComparator`

(

- `node`

)
Number | String

Defined in `tree/js/extensions/tree-sortable.js:69`

Default comparator function to use when sorting a node's children if the node itself doesn't have a custom comparator function.

If not specified, insertion order will be used by default.

#### Parameters:

- `node` Tree.Node

  Node being sorted.

#### Returns:

Number | String: Value by which the node should be sorted relative to its siblings.

### `sortNode`

(

- `node`
- `[options]`

)
chainable

Defined in `tree/js/extensions/tree-sortable.js:84`

Sorts the children of the specified node.

#### Parameters:

- `node` Tree.Node

  Node whose children should be sorted.

- `[options]` Object optional

  Options.

  - `[silent]` Boolean optional

    If `true`, no `sort` event will be fired.

  - `[sortComparator]` Function optional

    Custom comparator function to use. If specified, this will become the node's new comparator function, overwriting any previous comparator function that was set for the node.

  - `[sortReverse]` Boolean optional

    If `true`, children will be sorted in reverse (descending) order. Otherwise they'll be sorted in ascending order. This will become the node's new sort order, overwriting any previous sort order that was set for the node.

  - `[src]` String optional

    Source of the sort operation. Will be passed along to the `sort` event facade.

## Properties

### `sortReverse`

Boolean

Defined in `tree/js/extensions/tree-sortable.js:43`

If `true`, node children will be sorted in reverse (descending) order by default. Otherwise they'll be sorted in ascending order.

Default: false

## Events

### `sort`

Defined in `tree/js/extensions/tree-sortable.js:27`

Fired after a node's children are re-sorted.

#### Event Payload:

- `node` Tree.Node

  Node whose children were sorted.

- `reverse` Boolean

  `true` if the children were sorted in reverse (descending) order, `false` otherwise.

- `src` String

  Source of the event.