Jump to content
Sign in to follow this  
GATC

Sort a 3D array

Recommended Posts

GATC

Hi.

I got a little problem here, I need to sort a 3D array.

My array is like that:

$myArray=[$i][$e][2]

$i and $e are variable.

I have data in the [$i][$e][0] and [$i][$e][1] slot.

What I need is to have my [$i][$e][1] sorted with increasing value with the modification around the $i

Example:

Before sorting

[1][6][1] = 16

[2][6][1] = 12

[3][6][1] = ""

[8][6][1] = 14

[9][6][1] = ""

[10][6][1]=14

[12][6][1] = ""

[14][6][1]=15

[15][6][1]=13

After sorting

[1][6][1] = 12

[2][6][1[ = 13

[3][6][1] = 14

[4][6][1] = 14

[5][6][1] = 15

[6][6][1] = 16

 

If two value are the same, they are sorted by the [$i][$e][0] value (and "ligne" with "" value are suppressed)

Hope I'm clear ^^'

Thx for "any" answer

Share this post


Link to post
Share on other sites
FireFox

Hi,

Welcome to the autoit forum :)

A quick answer: edit the _ArraySort function in order to work on the 3rd dimension.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Malkey

3D arrays are seldom used. Existing array functions are geared up for 2D arrays.

As an exercise here are two examples of 3D array related functions.

Note: The 3D arrays $aArr[$i][$e][0] and $aArr[$i][$e][1] are actually 2D arrays each with its own unique constant third dimension index.

#include <Array.au3>

Local  $aArr[16][7][2]

$aArr[1][6][1] = 16
$aArr[2][6][1] = 12
$aArr[3][6][1] = ""
$aArr[8][6][1] = 14
$aArr[9][6][1] = ""
$aArr[10][6][1] = 14
$aArr[12][6][1] = ""
$aArr[14][6][1] = 15
$aArr[15][6][1] = 13
Local $aArr1 = $aArr ; For 2nd example

;Extract 2D array from 3D array, $aArr[$i][$e][1].
Local $aTemp2D[UBound($aArr, 1)][UBound($aArr, 2)]
For $i = 0 To UBound($aArr, 1) - 1
    For $e = 0 To UBound($aArr, 2) - 1
        $aTemp2D[$i][$e] = $aArr[$i][$e][1]
    Next
Next

_ArraySort($aTemp2D, 0, 0, 0, 6)
_ArrayDisplay($aTemp2D, "Sorted 2D Array of 3D array")

;Copy 2D array, $aTemp2D[$i][$e], back into 3D array, $aArr[$i][$e][1].
For $i = 0 To UBound($aArr, 1) - 1
    For $e = 0 To UBound($aArr, 2) - 1
        $aArr[$i][$e][1] = $aTemp2D[$i][$e]
    Next
Next

;------------------ 2nd Example --------------------------------------
;Or

Local $Flag = 0, $Temp, $sRes = ""

_Sort1DimOf3DArray($aArr1, 6, 1, 1, 1)
MsgBox(0, "Sorted 1D of 3D array", _Display1Dimof3DArray($aArr1, 6, 1))


; Sorts only one dimension in a 3 dimensional array.
Func _Sort1DimOf3DArray(ByRef $aArr, $i2D = 0, $i3D = 0, $Ascend = 1, $Numeric = 1)
    If $i2D > UBound($aArr, 2) - 1 Then $i2D = UBound($aArr, 2) - 1
    If $i3D > UBound($aArr, 3) - 1 Then $i3D = UBound($aArr, 3) - 1
    While $Flag = 0
        $Flag = 1
        For $i = 1 To UBound($aArr, 1) - 1
            If ($Numeric = 1 and $Ascend = 1 and Number($aArr[$i - 1][$i2D][$i3D]) > Number($aArr[$i][$i2D][$i3D])) or _ ; Sort Numerically ascending
               ($Numeric = 1 and $Ascend = 0 and Number($aArr[$i - 1][$i2D][$i3D]) < Number($aArr[$i][$i2D][$i3D])) or _ ; Sort Numerically descending
               ($Numeric = 0 and $Ascend = 1 and $aArr[$i - 1][$i2D][$i3D] > $aArr[$i][$i2D][$i3D]) or _           ; Sort Alpha-numerically ascending
               ($Numeric = 0 and $Ascend = 0 and $aArr[$i - 1][$i2D][$i3D] < $aArr[$i][$i2D][$i3D]) Then           ; Sort Alpha-numerically descending
                $Flag = 0
                $Temp = $aArr[$i - 1][$i2D][$i3D]
                $aArr[$i - 1][$i2D][$i3D] = $aArr[$i][$i2D][$i3D]
                $aArr[$i][$i2D][$i3D] = $Temp
            EndIf
        Next
    WEnd
EndFunc   ;==>_Sort1DimOf3DArray

; Displays only one dimension of a 3 dimensional array.
Func _Display1Dimof3DArray(ByRef $aArr, $i2D = 0, $i3D = 0)
    If $i2D > UBound($aArr, 2) - 1 Then $i2D = UBound($aArr, 2) - 1
    If $i3D > UBound($aArr, 3) - 1 Then $i3D = UBound($aArr, 3) - 1
    For $i = 0 To UBound($aArr, 1) - 1
        ;$sRes &= "[" & $i & "][$i2D][$i3D] = " & $aArr[$i][$i2D][$i3D] & @CRLF
        $sRes &= "[" & $i & "][" & $i2D & "][" & $i3D & "] = " & $aArr[$i][$i2D][$i3D] & @CRLF
    Next
    Return $sRes
EndFunc   ;==>_Display1Dimof3DArray
Edit: This line:-

$sRes &= "[" & $i & "][" & $i2D & "][" & $i3D & "] = " & $aArr[$i][$i2D][$i3D] & @CRLF

to replace previous now commented out line in _Display1Dimof3DArray function.

Edited by Malkey

Share this post


Link to post
Share on other sites
czardas

I had a think about this. The way I would approach it would be to first sort all stacked tables individually on the first column. Then stack the tables in order by sorting on the contents of the first cell in every table. Sorted on the first entry in the first dimension... $aArray[0][0][0] - $aArray[$z][0][0]. Interesting problem!

Edited by czardas

Share this post


Link to post
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
Sign in to follow this  

×