Sort array alphabetically - educational

fikira Wizard تم تحديثه   

    • This educational script will sort an array of tickers alphabetically and place these values in an table, together with the according current price value next to each ticker.


🔹   I. We make a User Defined Type (UDT) obj, with:

        ·  ticker - the string name of the ticker
        ·  price - the current price (close)

    • From this UDT we make an object for each ticker

🔹   II. 2 array's are made:

    • array of objects aObj, containing obj type for every ticker
    • array of strings sort, the ticker part of each object

🔹   III. Now we make an object of each ticker with the createObject(sym ) function

object_1 = createObject("TICKER")

    • the object object_1 consists off:
        ·  ticker -> "TICKER"
        ·  price -> current Daily close through"TICKER") (non-repainting)
    • object_1 will be added to the aObj array
    • "TICKER" (string ticker part of object) will be added to the sort array

🔹   IV. The latter array is sorted alphabetically by using array.sort_indices()


originalArray = array.from("B", "A", "C")
indicesArray = [1, 0, 2] // sorted indices

array.get(originalArray, 1) -> "A"
array.get(originalArray, 0) -> "B"
array.get(originalArray, 2) -> "C"


Alphabetically sorting is case sensitive, just like Java compareTo(String anotherString)!
    • The comparison is based on the Unicode value of each character in the string, the lowest "Dec" values are sorted first in line.
    • Comparing the "Dec" values at unicodelookup explains why default CAPITAL lettres will be sorted first,
    • Default you would get this (A= 65, B= 66, a= 97, b= 98)


    • Adding str.lower(string) in the toLowerCase() function will result to the following:


   • (A= 65 is transformed to a= 97, ...)

   • As a side note, should you write "AMZN" as "ÀMZN" this would be placed at the end, even after transforming to lower case the "Dec" values are higher (À= 192, à= 224).

    • You can toggle "To Lower Case" to verify.

🔹   V. Values are placed in a table, using these sorted indices.

    • With the usage of UDTs and objects, the current price has the same index in the aObj as their ticker,
                  giving the advantage it is fairly easy to place every value correctly next to each other.
    • The same can be done by make 2 separate arrays, 1 for the current price, the other for "TICKER".


    • Alternative technique for adding comment

        Instead of
//   this is a comment   

        You can also do this:
_="   this is a comment  "

    • Alternate colour

        ·  During a loop, alternate colour when i is even or odd, using the modulo operation (%).
        ·  This is the remainder when dividing.


        ·  3 % 2 = 1 -> 3 / 2 -> 1 * 2, 1 left (remainder)
        ·  4 % 2 = 0 -> 4 / 2 -> 2 * 2, 0 left (remainder)
        ·  5 % 2 = 1 -> 5 / 2 -> 2 * 2, 1 left (remainder)

for i = 0 to 10
    even  =  i  %  2  ==  0 
    col     = even ? thisColor : otherColor

    • Adjust colour in script by using colour picker

ملاحظات الأخبار:
Added "ignore_invalid_symbol= true" in "" function

- We cannot control our emotions,
but we can control our keyboard -
نص برمجي مفتوح المصدر

قام مؤلف هذا النص البرمجي بنشره وجعله مفتوح المصدر، بحيث يمكن للمتداولين فهمه والتحقق منه، وهو الأمر الذي يدخل ضمن قيم TradingView. تحياتنا للمؤلف! يمكنك استخدامه مجانًا، ولكن إعادة استخدام هذا الكود في منشور تحكمه قواعد الموقع. يمكنك جعله مفضلاً لاستخدامه على الرسم البياني.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.

هل تريد استخدام هذا النص البرمجي على الرسم البياني؟