Jump to content

Sorting 2D array by custom params


Recommended Posts

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

So this works, but I would like to ask you if there is a more intelligent way for doing this.

#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

Hello :) 

I am not familiar with it... So i wont provide you somthing else than the help file exemple's :P 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 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 - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

  • Moderators
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 :P 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

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 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 - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

  • Moderators

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

  • Moderators

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

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

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 by pixelsearch
Link to comment
Share on other sites

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

For the fun   :P

#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

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