PunkoHead Posted August 30, 2018 Share Posted August 30, 2018 Hi Guys, Is there a way for me to sort a 2D array via custom parameter? I have the following array Global $aArray[5][3] $aArray[0][0] = "1234" $aArray[0][1] = "low" $aArray[0][2] = '=HYPERLINK(STH,STH)' $aArray[1][0] = "2341" $aArray[1][1] = "medium" $aArray[1][2] = '=HYPERLINK(STH,STH)' $aArray[2][0] = "3412" $aArray[2][1] = "lowest" $aArray[2][2] = '=HYPERLINK(STH,STH)' $aArray[3][0] = "4123" $aArray[3][1] = "high" $aArray[3][2] = '=HYPERLINK(STH,STH)' $aArray[4][0] = "1235" $aArray[4][1] = "critical" $aArray[4][2] = '=HYPERLINK(STH,STH)' So this will be [1234][low][Some text] [2341][medium][Some text] [3412][lowest][Some text] [4123][high][Some text] [1235][critical][Some text] And now I want this to be sortet by the 2nd column starting from highest, going to lowest. The end result I am looking for is: [1235][critical][Some text] [4123][high][Some text] [2341][medium][Some text] [1234][low][Some text] [3412][lowest][Some text] Is there any way I can do this? Thanks in advance! Link to comment Share on other sites More sharing options...
PunkoHead Posted August 30, 2018 Author Share Posted August 30, 2018 So this works, but I would like to ask you if there is a more intelligent way for doing this. expandcollapse popup#include <Array.au3> $counter = 0 Global $aArray[5][3] $aArray[0][0] = "1234" $aArray[0][1] = "low" $aArray[0][2] = '=HYPERLINK(STH,STH)' $aArray[1][0] = "2341" $aArray[1][1] = "medium" $aArray[1][2] = '=HYPERLINK(STH,STH)' $aArray[2][0] = "3412" $aArray[2][1] = "lowest" $aArray[2][2] = '=HYPERLINK(STH,STH)' $aArray[3][0] = "4123" $aArray[3][1] = "high" $aArray[3][2] = '=HYPERLINK(STH,STH)' $aArray[4][0] = "1235" $aArray[4][1] = "critical" $aArray[4][2] = '=HYPERLINK(STH,STH)' _ArrayDisplay($aArray) Global $bArray[5][3] For $x=0 To 4 If $aArray[$x][1] = "critical" Then $bArray[$counter][0] = $aArray[$x][0] $bArray[$counter][1] = $aArray[$x][1] $bArray[$counter][2] = $aArray[$x][2] $counter = $counter+1 _ArrayDisplay($bArray) EndIf Next For $x = 0 To 4 If $aArray[$x][1] = "high" Then $bArray[$counter][0] = $aArray[$x][0] $bArray[$counter][1] = $aArray[$x][1] $bArray[$counter][2] = $aArray[$x][2] $counter = $counter+1 _ArrayDisplay($bArray) EndIf Next For $x = 0 To 4 If $aArray[$x][1] = "medium" Then $bArray[$counter][0] = $aArray[$x][0] $bArray[$counter][1] = $aArray[$x][1] $bArray[$counter][2] = $aArray[$x][2] $counter = $counter+1 _ArrayDisplay($bArray) EndIf Next For $x = 0 To 4 If $aArray[$x][1] = "low" Then $bArray[$counter][0] = $aArray[$x][0] $bArray[$counter][1] = $aArray[$x][1] $bArray[$counter][2] = $aArray[$x][2] $counter = $counter+1 _ArrayDisplay($bArray) EndIf Next For $x = 0 To 4 If $aArray[$x][1] = "lowest" Then $bArray[$counter][0] = $aArray[$x][0] $bArray[$counter][1] = $aArray[$x][1] $bArray[$counter][2] = $aArray[$x][2] $counter = $counter+1 _ArrayDisplay($bArray) EndIf Next _ArrayDisplay($bArray) Link to comment Share on other sites More sharing options...
caramen Posted August 30, 2018 Share Posted August 30, 2018 (edited) Hello I am not familiar with it... So i wont provide you somthing else than the help file exemple's But if i am not wrong you can do that with this command : _ArraySort ; using a 2D array #include <Array.au3> Local $avArray[5][3] = [ _ [5, 20, 8], _ [4, 32, 7], _ [3, 16, 9], _ [2, 35, 0], _ [1, 19, 6]] _ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()") _ArraySort($avArray, 0, 0, 0, 0) _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 0") _ArraySort($avArray, 0, 0, 0, 1) _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 1") _ArraySort($avArray, 0, 0, 0, 2) _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 2") Cheers Edited August 30, 2018 by caramen My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 30, 2018 Moderators Share Posted August 30, 2018 22 minutes ago, caramen said: Hello I am not familiar with it... So i wont provide you somthing else than the help file exemple's But if i am not wrong you can do that with this command : As what you provide doesn't go toward answering his question, if you don't know (as you yourself state) how about just staying out of the topic rather than commenting needlessly? "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
caramen Posted August 30, 2018 Share Posted August 30, 2018 (edited) He try to sort array i give him a command to sort array what is the problem ? Btw trying to help even in a wrong way help people to understand... and it help myself too. Edited August 30, 2018 by caramen My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 30, 2018 Moderators Share Posted August 30, 2018 Not sure how "helping in the wrong way" does anything but boost your post count. Simply put, as you state yourself you don't understand what the OP is trying to accomplish - if that is the case please do not pollute the thread with useless posts. @PunkoHead I believe, unfortunately, you are on the write track with looping through the array. Unlike javascript or other languages, there really isn't anything native to sort on a custom value; you would have to write your own function. I am not somewhere where I can play with the code, but as you loop through the array looking for the Value at each level adding the row to a new array may be faster. Someone may wander along with suggestions on a faster search. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 30, 2018 Moderators Share Posted August 30, 2018 PunkoHead, Try using my ArrayMultiColSort UDF (the link is in my sig below) - this works for me: #include "ArrayMultiColSort.au3" Global $aArray[][] = [["1234", "low", '=HYPERLINK(STH,STH)'], _ ["2341", "medium", '=HYPERLINK(STH,STH)'], _ ["3412", "lowest", '=HYPERLINK(STH,STH)'], _ ["4123", "high", '=HYPERLINK(STH,STH)'], _ ["1235", "critical", '=HYPERLINK(STH,STH)']] _ArrayDisplay($aArray, "Unsorted") ; Create sort data array ; Col 1 - Required order of elements Global $aSortData[][] = [[1, "critical,high,medium,low,lowest"]] ; Sort and display array _ArrayMultiColSort($aArray, $aSortData) ; Display any errors encountered If @error Then ConsoleWrite("Oops: " & @error & " - " & @extended & @CRLF) _ArrayDisplay($aArray, "Sorted") And notice the way to declare and fill an array - a bit less finger wear than yours! M23 JLogan3o13 1 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 August 30, 2018 Share Posted August 30, 2018 (edited) Glad we have Melba here, he wrote some nice Array UDF's Without his UDF, I would have added an empty column on each row (a future 'sort column') then populate it like this : #include <Array.au3> Global $aArray[][] = [["1234", "low", '=HYPERLINK(STH,STH)', 0], _ ["2341", "medium", '=HYPERLINK(STH,STH)', 0], _ ["3412", "lowest", '=HYPERLINK(STH,STH)', 0], _ ["4123", "high", '=HYPERLINK(STH,STH)', 0], _ ["1235", "critical", '=HYPERLINK(STH,STH)', 0]] _ArrayDisplay($aArray, "Unsorted" , "|2") ; hide last column $sStatus = "5=critical 4=high 3=medium 2=low 1=lowest" For $i = 0 to UBound($aArray) - 1 $iPos = StringInStr($sStatus, $aArray[$i][1]) If $iPos <> 0 Then $aArray[$i][3] = StringMid($sStatus, $iPos - 2, 1) Else MsgBox(0, "Row " & $i, "Status not found : " & $aArray[$i][1]) Exit EndIf Next _ArraySort($aArray, 1, 0, 0, 3) ; 1 = descending . 3 = sort on column 3 _ArrayDisplay($aArray, "Sorted" , "|2") ; hide last column Edited August 30, 2018 by pixelsearch caramen 1 Link to comment Share on other sites More sharing options...
pixelsearch Posted August 30, 2018 Share Posted August 30, 2018 The help file states that "Function calls can also be placed in the initializers of an array" As I never used it, now is the time to give it a try... and it works nicely #include <Array.au3> Global $sStatus = "5=critical 4=high 3=medium 2=low 1=lowest", $iRow = -1 Global $aArray[][] = [["1234", "low", '=HYPERLINK(STH,STH)', _Calc()], _ ["2341", "medium", '=HYPERLINK(STH,STH)', _Calc()], _ ["3412", "lowest", '=HYPERLINK(STH,STH)', _Calc()], _ ["4123", "high", '=HYPERLINK(STH,STH)', _Calc()], _ ["1235", "critical", '=HYPERLINK(STH,STH)', _Calc()]] _ArraySort($aArray, 1, 0, 0, 3) ; 1 = descending . 3 = sort on column 3 _ArrayDisplay($aArray, "Sorted" , "|2") ; hide last column Func _Calc() $iRow = $iRow +1 ; 0, 1, 2 etc... Local $iPos = StringInStr($sStatus, $aArray[$iRow][1]) If $iPos <> 0 Then Return StringMid($sStatus, $iPos -2, 1) Else MsgBox(0, "Row " & $iRow, "Status not found : " & $aArray[$iRow][1]) Exit EndIf EndFunc Link to comment Share on other sites More sharing options...
mikell Posted August 30, 2018 Share Posted August 30, 2018 For the fun #include <Array.au3> Global $aArray[][] = [["1234", "low", '=HYPERLINK(STH,STH)'], _ ["2341", "medium", '=HYPERLINK(STH,STH)'], _ ["3412", "lowest", '=HYPERLINK(STH,STH)'], _ ["4123", "high", '=HYPERLINK(STH,STH)'], _ ["1235", "critical", '=HYPERLINK(STH,STH)']] _ArrayDisplay($aArray, "Unsorted") For $i = 0 to UBound($aArray) - 1 $aArray[$i][1] = StringReplace($aArray[$i][1], "lo", "z") Next _ArraySort($aArray, 0, 0, 0, 1) For $i = 0 to UBound($aArray) - 1 $aArray[$i][1] = StringReplace($aArray[$i][1], "z", "lo") Next _ArrayDisplay($aArray, "Sorted") 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