Pine Script

Sorting an array in Pine Script

Sorting an array in Pine Script

Sorting is an essential feature of arrays in programming, as it allows the user to organize data easily. In this article, I’ll show you how you can quickly sort arrays in Pine Script.

First, let’s define an array we want to sort:

x = array.from(5, 2, 0, 1, 3)

The primary function to sort arrays in Pine Script is array.sort. You can simply pass your array to this function, and Pine Script will sort elements of your array in ascending order. Your array will be sorted “In Place”. You shouldn’t reassign it to the new variable.

array.sort(x)

To check the result of this sorting, I’ll plot the array in a label:

if (barstate.islast)
    label.new(bar_index, high, str.tostring(x))

You can see that it works well. We get an array sorted in ascending order:

To sort the array in descending order, you can simply use the second optional argument “order”:

array.sort(x, order.descending)

This will oppositely sort the array:

Sort_indices function

Another exciting function in Pine Script I have to show you is “array.sort_indices”. With its help, you can easily find the lowest/highest/the second lowest/etc. elements. Let’s see how it works with an example:

y = array.sort_indices(x)

So as you can immediately see, it doesn’t work “in place” as the array_sort function but outputs another array. This array will represent indices for your array from the lowest to the highest value. If you print it you’ll see the following array:

It’s pretty easy to interpret these numbers. The first value is 2, which means that the lowest value in your array is located at a position with index = 2, and the last value is 0, which means that the largest value is the first in your array.


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

2 thoughts on “Sorting an array in Pine Script”

  1. Hi, this was very helpful. I have 8 variables that I wanted to put in descending order and put into a table with 2 columns, Variable Name and Value.

    Once I have created:
    cur = array.from(eur,usd,gbp,jpy,aud,nzd,chf,cad)
    array.sort(cur, order.descending)

    How would I put these sorted values with the sorted variables into the table. Heres the table code I have without sorted Variables and Values:
    rst = table.new(position.bottom_right, 2, 9, frame_color=color.black, frame_width=2, border_color=color.black, border_width=2)
    table.cell(rst,0,0,”Currency”)
    table.cell(rst,1,0, “Volume Value”)
    table.cell(rst,0,1,”EUR”,text_color = eur>0 ? color.green : color.red)
    table.cell(rst,1,1, tostring(eur),text_color = eur>0 ? color.green : color.red)
    table.cell(rst,0,2,”USD”,text_color = usd>0 ? color.green : color.red)
    table.cell(rst,1,2, tostring(usd),text_color = usd>0 ? color.green : color.red)
    table.cell(rst,0,3,”GBP”,text_color = gbp>0 ? color.green : color.red)
    table.cell(rst,1,3, tostring(gbp),text_color = gbp>0 ? color.green : color.red)
    table.cell(rst,0,4,”JPY”,text_color = jpy>0 ? color.green : color.red)
    table.cell(rst,1,4, tostring(jpy),text_color = jpy>0 ? color.green : color.red)
    table.cell(rst,0,5,”AUD”,text_color = aud>0 ? color.green : color.red)
    table.cell(rst,1,5, tostring(aud),text_color = aud>0 ? color.green : color.red)
    table.cell(rst,0,6,”CAD”,text_color = cad>0 ? color.green : color.red)
    table.cell(rst,1,6, tostring(cad),text_color = cad>0 ? color.green : color.red)
    table.cell(rst,0,7,”CHF”,text_color = chf>0 ? color.green : color.red)
    table.cell(rst,1,7, tostring(chf),text_color = chf>0 ? color.green : color.red)
    table.cell(rst,0,8,”NZD”,text_color = nzd>0 ? color.green : color.red)
    table.cell(rst,1,8, tostring(nzd),text_color = nzd>0 ? color.green : color.red)

    Thank you!
    Barry

    1. If I understand you correctly you want to sort 2 arrays at the save time with values of 1 of them. (I consider you’re creating a array with names of the forex pairs)
      The easiest way would be to use array.sort_indices(x) to find the indices of values from the lowest to highest and then to go through them in a loop and pick correct values from each array to fill the table.
      I might create an article soon to explain that.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news