Jump to content

_arraysort() doesn't sort numbers correctly


Recommended Posts

Hi all,

I have the following function that will gather data from Crypto.com and should return me top 10/worst 10 assets:

Func _TopBad()
    AdlibRegister("_wait2", 1000)
    Local $aAssetDataPositive[0][2] ; name - diff%
    Local $aAssetDataNegative[0][2] ; name - diff%
    If $iDefaultExchange = "Crypto.com" Then
        For $i = 0 To UBound($aPublicAssets) - 1
            Local $sResult = CryptoQueryPublic("get-ticker", "instrument_name=" & $aPublicAssets[$i] & "_" & $iDefaultCurrency) ;Ticker
            $ask = _StringBetween($sResult, '"a":', ',')
            If IsArray($ask) Then
                $last = _StringBetween($sResult, '"a":', ',')
                $Change = _StringBetween($sResult, '"c":', '}')
                Local $delta = Round(((Number($last[0]) / (Number($last[0]) - Number($Change[0]))) - 1) * 100, 2)
                If StringInStr($delta, "-") = 0 Then ; positive
                    _ArrayAdd($aAssetDataPositive, $aPublicAssets[$i] & "|" & Number($delta))
                Else
                    _ArrayAdd($aAssetDataNegative, $aPublicAssets[$i] & "|" & Number($delta))
                EndIf
            Else
                _CW($aPublicAssets[$i] & ": " & $sResult)
            EndIf
        Next
    EndIf
    _ArraySort($aAssetDataPositive, 1, 1, 0, 1)
    _ArraySort($aAssetDataNegative, 1, 0, 0, 1)
    For $i = 0 To UBound($aAssetDataPositive) - 1
        If $aAssetDataPositive[$i][1] >= 5 Then
            GUICtrlSetColor($I_TopPerformers[$i][1], $verde)
        ElseIf $aAssetDataPositive[$i][1] >= 0 Then
            GUICtrlSetColor($I_TopPerformers[$i][1], $verdechiaro)
        ElseIf $aAssetDataPositive[$i][1] < 0 Then
            GUICtrlSetColor($I_TopPerformers[$i][1], $rosso)
        EndIf
        GUICtrlSetData($I_TopPerformers[$i][0], $aAssetDataPositive[$i][0])
        GUICtrlSetData($I_TopPerformers[$i][1], $aAssetDataPositive[$i][1] & "%")
        If $i = 9 Then ExitLoop
    Next
    For $i = 0 To UBound($aAssetDataNegative) - 1
        If $aAssetDataNegative[$i][1] <= -5 Then
            GUICtrlSetColor($I_BadPerformers[$i][1], $rosso)
        ElseIf $aAssetDataNegative[$i][1] <= 0 Then
            GUICtrlSetColor($I_BadPerformers[$i][1], $giallo)
        ElseIf $aAssetDataNegative[$i][1] > 0 Then
            GUICtrlSetColor($I_BadPerformers[$i][1], $verde)
        EndIf
        GUICtrlSetData($I_BadPerformers[$i][0], $aAssetDataNegative[$i][0])
        GUICtrlSetData($I_BadPerformers[$i][1], $aAssetDataNegative[$i][1] & "%")
        If $i = 9 Then ExitLoop
    Next
    _ArrayDisplay($aAssetDataPositive, "Positive")
    _ArrayDisplay($aAssetDataNegative, "Negative")
    AdlibUnRegister("_wait2")
    GUICtrlSetState($htoplabel, $GUI_HIDE)
    $showLabel2 = False
    GUISetState(@SW_SHOW, $FRM_TopPerformers)
    GUISetState(@SW_SHOW, $FRM_BadPerformers)
EndFunc   ;==>_TopBad

it should just pick best 10 performers and worst 10 performers and display on a GUI

but these are the results (just a subset of the _arraydisplay() where you can see numbers are not ordered)

screenshot_1797.jpg.7e7c4ed6dde7422fe4af46cbacbb8f98.jpg

So when the GUI is created here are the results (same for worst assets)

screenshot_1798.jpg.2364ad7d356ab2a0912a07c370d23c8d.jpg

Arrays are sorted before cycling

    _ArraySort($aAssetDataPositive, 1, 1, 0, 1)
    _ArraySort($aAssetDataNegative, 1, 0, 0, 1)

and I also added a

_ArrayAdd($aAssetDataPositive, $aPublicAssets[$i] & "|" & Number($delta))

but it seems not working.

Any suggestion?

Maybe @Melba23?, I also tried your MultiCol array sort but didn't work

Thanks,

Marco

Link to post
Share on other sites
  • Moderators

marko001,

Although you are using Number, you are also concatenating the various elements of the row using & - and so it all ends up as a string:

_ArrayAdd($aAssetDataNegative, $aPublicAssets[$i] & "|" & Number($delta))

You will need to add a further line after adding to the array specifically setting that element to the number datatype:

If StringInStr($delta, "-") = 0 Then ; positive
     $iRow = _ArrayAdd($aAssetDataPositive, $aPublicAssets[$i] & "|" & $delta)
 Else
     $iRow = _ArrayAdd($aAssetDataNegative, $aPublicAssets[$i] & "|" & $delta)
 EndIf
 ; Now set the element to Number datatype
 $aAssetDataPositive[$iRow][1] = Number($aAssetDataPositive[$iRow][1])

And my UDF uses the _ArraySort function and so, unsurprisingly, acts the same way.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites

@Melba23 It seems better but first row is still wrong

If StringInStr($delta, "-") = 0 Then ; positive
                    $iRow = _ArrayAdd($aAssetDataPositive, $aPublicAssets[$i] & "|" & $delta)
                    $aAssetDataPositive[$iRow][1] = Number($aAssetDataPositive[$iRow][1])
                Else
                    $iRow = _ArrayAdd($aAssetDataNegative, $aPublicAssets[$i] & "|" & $delta)
                    $aAssetDataNegative[$iRow][1] = Number($aAssetDataNegative[$iRow][1])
                EndIf

image.png.fc1e6a78de1b1b6187c6a9fed845720d.png

Negative it's whis way:

image.png.a968a081c0fc9b900433c1fe53bb751a.png

 

I used

_ArraySort($aAssetDataPositive, 1, 1, 0, 1)
    _ArraySort($aAssetDataNegative, 1, 0, 0, 1)

and here results:

image.png.b65c4755ec44d4d4b99f4015bf7040a9.png

If I use 1 and not it doesn't order at all but messes everything

image.png

image.png

Edited by marko001
Link to post
Share on other sites
  • Moderators

marko001,

This seems to work fine for me:

#include <Array.au3>

Global $aPublicAssets[] = ["1INCH", "KNC", "PLA", "FTM", "RNDR", "ATOM", "LRC", "GODS", _
                    "EGLD", "PAXG", "POWR", "BRZ", "DERC", "MIR", "SDN", "XYO", _
                    "BADGER", "PENDLE", "ANY", "PERP", "HOD","RADAR"]

; Save as strings
Global $aDelta[] = ["2.7", "24.04", "18.45", "18.26", "17.68", "17.63", "15.06", "14.78", _
                    "12.83", "-0.14", "-0.48", "0.72", "-1.33", "-1.51", "-1.93", "-3.31", _
                    "-4.62", "-4.93", "-5.25", "-9.24", "-10.41","-20.29"]

_ArrayShuffle($aDelta) ; Randomize the order

Global  $aAssetDataPositive[0][2], $aAssetDataNegative[0][2]

For $i = 0 tO UBound($aPublicAssets) - 1
    If StringInStr($aDelta[$i], "-") = 0 Then ; positive
        $iRow = _ArrayAdd($aAssetDataPositive, $aPublicAssets[$i] & "|" & $aDelta[$i])
        $aAssetDataPositive[$iRow][1] = Number($aAssetDataPositive[$iRow][1])
    Else
        $iRow = _ArrayAdd($aAssetDataNegative, $aPublicAssets[$i] & "|" & $aDelta[$i])
        $aAssetDataNegative[$iRow][1] = Number($aAssetDataNegative[$iRow][1])
    EndIf
Next

_ArrayDisplay($aAssetDataPositive, "Unsorted", Default, 8)
_ArraySort($aAssetDataPositive, 1, 0, 0, 1)
_ArrayDisplay($aAssetDataPositive, "Sorted", Default, 8)

_ArrayDisplay($aAssetDataNegative, "Unsorted", Default, 8)
_ArraySort($aAssetDataNegative, 1, 0, 0, 1)
_ArrayDisplay($aAssetDataNegative, "Sorted", Default, 8)

I have randomized the delta values because copying the lists from your post above meant that the arrays were already sorted.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites
  • Moderators

marko001,

Glad I could help.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...