cyxstudio Posted October 27, 2018 Share Posted October 27, 2018 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 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 More sharing options...
Moderators Melba23 Posted October 27, 2018 Moderators Share Posted October 27, 2018 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted October 27, 2018 Share Posted October 27, 2018 @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: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
pixelsearch Posted October 27, 2018 Share Posted October 27, 2018 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 More sharing options...
Moderators Melba23 Posted October 27, 2018 Moderators Share Posted October 27, 2018 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
pixelsearch Posted October 27, 2018 Share Posted October 27, 2018 (edited) 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 October 27, 2018 by pixelsearch Oops, made the mistake again, ternary requires ? : and not : ? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 27, 2018 Moderators Share Posted October 27, 2018 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted October 27, 2018 Share Posted October 27, 2018 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 More sharing options...
Moderators Melba23 Posted October 27, 2018 Moderators Share Posted October 27, 2018 (edited) 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 October 27, 2018 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now