Sidechair

matrix.sort()

Browse the reference

Reorders the rows of a matrix by the values in one column.

Try it in the editor

Each row moves as a whole; the column chosen only decides the order. By default column 0 is used and the smallest value comes first. Pass order.descending to put the largest first.

It works on numeric and string matrices and sorts in place.

Syntax

Pine Script
matrix.sort(id: matrix<int/float/string>, column: series int (optional), order: series sort_order (optional)) -> void
matrix.sort(id: any matrix type, column: series int (optional), order: series sort_order (optional), sort_field: const int/string (optional)) -> void
matrix.sort(column: series int (optional), order: series sort_order (optional)) -> void
matrix.sort(column: series int (optional), order: series sort_order (optional), sort_field: const int/string (optional)) -> void

Arguments

NameTypeDefaultDescription
idmatrix<int/float/string> or any matrix typerequiredThe matrix whose rows are put in order by the values of one column.
columnseries int0Index of the column. Counts from 0.
orderseries sort_orderorder.ascendingorder.ascending or order.descending.
sort_fieldconst int/stringnaThe field of a user-defined type to sort by when the matrix holds objects.

Returns

Nothing.

Example

Pine ScriptRank rows by scoreOpen in editor
//@version=6
indicator("matrix.sort: by score", overlay = false)
matrix<float> m = matrix.new<float>(0, 2)
matrix.add_row(m, 0, array.from(3.0, 10.0))
matrix.add_row(m, 1, array.from(1.0, 20.0))
matrix.sort(m, 0, order.ascending)
plot(matrix.get(m, 0, 1), "Second field of the smallest row")

See also

Runs on Sidechair