Jump to content

_arraysort acting weirdly, not sorting properly


Recommended Posts

I wrote a script that gives me the below data

 

Local $agent[0][2]

;--------------scrapping data and end up with the 2D array as below -------------------

[68, 2], [126, 7], [181, 7], [11, 8]]

but when i sort it with 

_ArraySort($agent, 0, 0, 0, 0)

this is what i got 

D4tiucU.png

 

why is 68 at the bottom? 68 should be in between 11 and 126.

isnt it suppose to sort based on col 0 ?

Link to comment
Share on other sites

  • Moderators

cyxstudio,

As has been explained many times the _ArraySort  function sorts alphabetically on strings and numerically on numbers - you appear to have the elements stored as strings. Loop through the array and convert them all to numbers (using Number or Int) and you will get the sort order you require.

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 comment
Share on other sites

@cyxstudio
This should do the trick:

#include <Array.au3>

Global $2D_Array[4][2] = [[68, 2], [126, 7], [181, 7], [11, 8]]


_ArraySort($2D_Array, 0, 0, 0, 0)
If @error Then Exit ConsoleWrite("Error while sorting the array. Error: " & @error & @CRLF)

_ArrayDisplay($2D_Array)

And, as Melba23 stated, if you are storing string elements in your array, you should convert them into number before doing the sort.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Also, as explained in the help file :
"In AutoIt there is only one datatype called a Variant. A variant can contain numeric or string data and decides how to use the data depending on the situation it is being used in. For example, if you try and multiply two variants they will be treated as numbers, if you try and concatenate (join) two variants they will be treated as strings." etc... etc...

Anyone knows who decided this when AutoIt was created ?
I mean, is it common in a programming language to have only one datatype ?
Thanks for your explanations :)

 

Link to comment
Share on other sites

  • Moderators

pixelsearch,

It was a design choice by Jon when he first wrote the language - I believe it was for simplicity of use for inexperienced users - and it is certainly too late to change it now. To try and prevent confusion we recommend that people name variables using a form of Hungarian notation (as explained here) so as to remind them of what exactly is stored in each one - but we still get many queries about sorting and comparison problems where datatype mismatch means that the result is not what is expected.

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 comment
Share on other sites

Thanks Melba for explaining, Jon can be proud of what he achieved, with the help of all of you
Wish I was with Jon when he created the "ternary" operator, I'd fight him to find an easier syntax, without these required : ? and parentheses everywhere, you better not forget them :)

I remember in Foxpro, we had a function that did same, with easier syntax, named iif() , pronounced "immediate if", it went like this :

iif(exprL, expr1, expr2)
If exprL was evaluated true, then result was expr1, if not, expr2

Edited by pixelsearch
Oops, made the mistake again, ternary requires ? : and not : ?
Link to comment
Share on other sites

  • Moderators

pixelsearch,

We are getting very much off-topic, but it was not Jon who introduced the ternary operator - that was trancexx when she was a developer some years ago. I must say I have no particular problem with the syntax - nor the parentheses, as although they are not always necessary I find them useful to delineate the various sections.

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 comment
Share on other sites

1 hour ago, Melba23 said:

As has been explained many times ...

So couldn't it be a good idea to add one single line in the help file to mention this ?
" convert elements to numbers to sort numerically" ... or so


pixelsearch,
Formerly the _Iif() function was present in AutoIt.
Replacing this func by an operator seems relevant to me, and I - personally - find the syntax very understandable  :)

Link to comment
Share on other sites

  • Moderators

mikell,

I was just thinking the same thing - I will add something to the Help file for the next release.

M23

Edit: And done.

Edited by Melba23

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 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...