Sorts an array of numbers or strings in place, ascending by default or descending when asked.
array.sort(a) puts the smallest item first; array.sort(a, order.descending) puts the largest first. Strings sort alphabetically by character code.
The array itself changes and nothing is returned. Sort a copy when the original order matters, or use array.sort_indices() to get the order without moving the items.
Syntax
array.sort(id: array<int/float/string>, order: series sort_order (optional)) -> void
array.sort(id: any array type, order: series sort_order (optional), sort_field: const int/string (optional)) -> void
array.sort(order: series sort_order (optional)) -> void
array.sort(order: series sort_order (optional), sort_field: const int/string (optional)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | array<int/ | required | The array to reorder in place, numbers or strings, by the order argument. |
| 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 array holds objects. |
Returns
Nothing.
Remarks
- On Sidechair: na items always end up last, in ascending and descending order alike.
- On Sidechair: the sort_field argument for arrays of user-defined types is not applied yet.
Example
//@version=6
indicator("array.sort: volume rank", overlay = false)
var vols = array.new_float()
array.push(vols, volume)
if array.size(vols) > 5
array.shift(vols)
ranked = array.copy(vols)
array.sort(ranked, order.descending)
plot(array.get(ranked, 0), "Largest of 5")
plot(array.get(ranked, -1), "Smallest of 5", color.gray)See also
Runs on Sidechair