Jump to content

_ArraySort is Not working correctly ?


Go to solution Solved by water,

Recommended Posts

Hello Everyone 

I have a strange issue  ...

The json generated array is not getting sorted properly for some reason 

 

#include <json.au3>
#include <Array.au3>


Dim $Chart[0][2] ; Array size of 2 columns


$object = json_decode(FileRead("Test.json")) ;for local testing
$Coins = json_get($object, '.data')


for $i = 0 to UBound($Coins) -1
    $name = json_get($object, '.data[' & $i & '].name' )
    $change_1h = json_get($object, '.data[' & $i & '].quote.USD.percent_change_1h')

    _ArrayAdd($Chart,$name & "|" & number($change_1h,3) )
Next


_ArraySort($Chart, 1, Default, Default, 1)
_ArrayDisplay($Chart)

 

_ArraySort  is sorting the array like this (wrong): 

image.png.0537fa3bf43154ece6f5595532e98f5d.png

 

 

However if i click on the "Col 1" header it will be sorted correctly:

 

image.png.8964bfb5bcc05a3770ee5faf38691864.png

 

 

How can i make _ArraySort  sort them like clicking on "Col 1" does ? 

 

PS: i tried using the function "Number()" when i add the value to the array , but same result ..

Thanks in advance 

Test.json

Edited by fraizor
Link to comment
Share on other sites

  • fraizor changed the title to _ArraySort is Not working correctly ?
  • Solution

This code returns a String as AutoIt internally converts your number to a String again to be able to concatenate them.

$name & "|" & number($change_1h,3)

Call _ArrayAdd twice to fill the two columns.

See the help file for details: "String datatype elements are sorted alphabetically and number datatype elements are sorted numerically - it is important to ensure that elements are of the correct datatype before sorting."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

4 minutes ago, water said:

This code returns a String as AutoIt internally converts your number to a String again to be able to concatenate them.

$name & "|" & number($change_1h,3)

Call _ArrayAdd twice to fill the two columns.

See the help file for details: "String datatype elements are sorted alphabetically and number datatype elements are sorted numerically - it is important to ensure that elements are of the correct datatype before sorting."

@waterThank you for your response 
i tried to Call _ArrayAdd twice, but it created spaces in the array ...

 

_ArrayAdd($Chart,$name)
    _ArrayAdd($Chart, $change_1h,1 )

Still after adding each value alone it is still not sorting properly  ..

image.png.aef851d2a338faec809135aa9b8a920d.png


 

Link to comment
Share on other sites

#include "json.au3"
#include <Array.au3>


Dim $Chart[0][5] ; Array size of 2 columns


$object = json_decode(FileRead("Test.json")) ;for local testing
$Coins = json_get($object, '.data')


for $i = 0 to UBound($Coins) -1
    $name = json_get($object, '.data[' & $i & '].name' )
    $change_1h = json_get($object, '.data[' & $i & '].quote.USD.percent_change_1h')

    _ArrayAdd($Chart,$name & "|" & number($change_1h,3) )

    $Chart[$i][2] = VarGetType ( $Chart[$i][1] )
    $Chart[$i][3] = number($change_1h,3)
    $Chart[$i][4] = VarGetType ( $Chart[$i][3] )
Next


_ArraySort($Chart, 1, Default, Default, 3)
_ArrayDisplay($Chart)

 

Edited by ioa747
corection _ArraySort($Chart, 1, Default, Default, 3)

I know that I know nothing

Link to comment
Share on other sites

I see. Then I suggest:

Global $iRow 

; Loop starts here
  $iRow = _ArrayAdd($Chart,$name)
  $Chart[$iRow][1] = number($change_1h,3)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Streamlined :

#include "C:\Apps\AutoIt\JSON\json.au3"
#include <Array.au3>

$object = json_decode(FileRead("Test.json")) ;for local testing
$Coins = json_get($object, '.data')

Local $Chart[UBound($Coins)][2]

for $i = 0 to UBound($Coins) -1
    $Chart[$i][0] = json_get($object, '.data[' & $i & '].name' )
    $Chart[$i][1] = Number(json_get($object, '.data[' & $i & '].quote.USD.percent_change_1h'), 3)
Next

_ArraySort($Chart, 3, Default, Default, 1)
_ArrayDisplay($Chart)

 

Link to comment
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
 Share

×
×
  • Create New...