Sort array alphabetically - educational
fi(ki)=>'ra' // ©fikira - This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator   (title="Sort array alphabetically"    ,   overlay= true)


_=" –––––––––––{ A.                    INPUT                       }–––––––––––  "

toLowCase         = input.bool(defval= true, title= "To Lower Case")

 
_=" –––––––––––{ A.            User Defined Type (UDT)             }–––––––––––  "

type obj
    string ticker = na
    float  price  = na 


_=" –––––––––––{ B.                    Array's                     }–––––––––––  "

aObj = array.new  <obj> ()
sort = array.new<string>()


_=" –––––––––––{ C.     Function which creates an object 'obj'     }–––––––––––  "

createObject(sym) => obj.new(ticker= sym, price= request.security(sym, 'D', close[1], barmerge.gaps_off, barmerge.lookahead_on, true))
toLowerCase (str) => toLowCase ? str.lower(str) : str


_=" –––––––––––{ D.     Create Objects,     add to array's         }–––––––––––  "
_=" –––––––––––{           1. createObject()                       }–––––––––––  "
_=" –––––––––––{           2. add object        to array 'aObj'    }–––––––––––  "
_=" –––––––––––{           3. add object.ticker to array 'sort'    }–––––––––––  "
_=" –––––––––––{                 -> with option to lower  case     }–––––––––––  "

GOOG         = createObject("GOOG"   ), array.unshift(aObj, GOOG  ), array.unshift(sort, toLowerCase(GOOG.ticker  ))
AMZN         = createObject("AMZN"   ), array.unshift(aObj, AMZN  ), array.unshift(sort, toLowerCase(AMZN.ticker  ))
GME          = createObject("GME"    ), array.unshift(aObj, GME   ), array.unshift(sort, toLowerCase(GME.ticker   ))
ETH          = createObject("ETHUSDT"), array.unshift(aObj, ETH   ), array.unshift(sort, toLowerCase(ETH.ticker   ))
AAPL         = createObject("AAPL"   ), array.unshift(aObj, AAPL  ), array.unshift(sort, toLowerCase(AAPL.ticker  ))
gold         = createObject("gold"   ), array.unshift(aObj, gold  ), array.unshift(sort, toLowerCase(gold.ticker  ))
SILK         = createObject("SILK"   ), array.unshift(aObj, SILK  ), array.unshift(sort, toLowerCase(SILK.ticker  ))
TSLA         = createObject("TSLA"   ), array.unshift(aObj, TSLA  ), array.unshift(sort, toLowerCase(TSLA.ticker  ))
BTC          = createObject("BTCUSDT"), array.unshift(aObj, BTC   ), array.unshift(sort, toLowerCase(BTC.ticker   ))
silver       = createObject("silver" ), array.unshift(aObj, silver), array.unshift(sort, toLowerCase(silver.ticker))
ROKU         = createObject("ROKU"   ), array.unshift(aObj, ROKU  ), array.unshift(sort, toLowerCase(ROKU.ticker  ))


_=" –––––––––––{ E.      Sort array 'sort' alphabetically          }–––––––––––  "

sort_Indices = array.sort_indices(id= sort, order= order.ascending) 


_=" –––––––––––{ F.         Place sorted data in table             }–––––––––––  "

if barstate.islastconfirmedhistory
    arr_Size = array.size(id= aObj)
    var tabl = table.new (position= position.top_right, columns= 2, rows= arr_Size)
    for i    = 0 to arr_Size -1
        even = i    %  2  ==  0 
        cBg  = even ?  #43ffdd : #8e43ff
        cTx  = even ?  #971010 : #e4fffc
        get  = array.get(id= aObj, index =    array.get(id= sort_Indices, index=i))
        table.cell(table_id= tabl, column= 0, row= i, text=              get.ticker, text_font_family=font.family_monospace, text_color= cTx, bgcolor= cBg)
        table.cell(table_id= tabl, column= 1, row= i, text= str.tostring(get.price), text_font_family=font.family_monospace, text_color= cTx, bgcolor= cBg)


_=" –––––––––––{ G.                     END                        }–––––––––––  "
