Reorders the rows of a matrix by the values in one column.
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
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)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | matrix<int/ | required | The matrix whose rows are put in order by the values of one column. |
| column | series int | 0 | Index of the column. Counts from 0. |
| order | series sort_order | order. | order.ascending or order.descending. |
| sort_field | const int/ | na | The field of a user-defined type to sort by when the matrix holds objects. |
Returns
Nothing.
Example
//@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