Array sortieren

  • Hallo AutoIt'ler,

    ich verzweifel im Moment an einem Problem.
    Und zwar möchte ich ein 2D,Array sortieren (an und für sich ja einfach) jedoch nicht einfach nur irgendeinen Subindex, sondern folgendermaßen.

    Ich habe z.B. Folgendes Array

    [["Gruppe 1", 20, 8], _
    ["Gruppe 2", 32, 7], _
    ["Gruppe 2", 16, 9], _
    ["Gruppe 2", 34, 7], _
    ["Gruppe 2", 122, 9], _
    ["Gruppe 1", 35, 0], _
    ["Gruppe 3", 19, 6]]

    Wenn ich es nun sortiere soll zunächst Subindex 0 und dann Subindex 1 sortiert werden. Sodass die Gruppen beieinander sind und Subindex 1 aufsteigend sortiert ist.
    Aber beim Versuch dies mit _arraysort zu realisieren bin ich gescheitert:

    [autoit]

    #include "array.au3"
    Local $avArray[7][3] = [ _
    ["Gruppe 1", 20, 8], _
    ["Gruppe 2", 32, 7], _
    ["Gruppe 2", 16, 9], _
    ["Gruppe 2", 34, 7], _
    ["Gruppe 2", 122, 9], _
    ["Gruppe 1", 35, 0], _
    ["Gruppe 3", 19, 6]]

    [/autoit][autoit][/autoit][autoit]

    _ArraySort($avArray,0,0,0,1)
    _ArrayDisplay($avArray)
    _ArraySort($avArray)
    _ArrayDisplay($avArray)

    [/autoit]

    Heraus kommt dann leider folgendes:

    [Blockierte Grafik: http://img827.imageshack.us/img827/74/arraysort.png]

    Habe es schon einmal versucht, indem ich es in ein neues Array schreibe. Da passte aber irgendwie garnichts mehr ;).
    Google hat mir auch nicht weitergeholfen. Nun wollte ich mich an euch wenden.
    Hoffe es hat jemand, wenn auch nur ansatzweise eine Idee wie ich es anstellen kann.

    Vielen Dank schon einmal.

    Gruß
    Maha

    21 is only half the truth.

    Einmal editiert, zuletzt von Mahagon (5. April 2011 um 14:28)

  • Meinst du so was?:

    Spoiler anzeigen
    [autoit]


    #include <Array.au3>
    Global $avArray[7][3] = [ _
    ["Gruppe 1", 20, 8], _
    ["Gruppe 2", 32, 7], _
    ["Gruppe 2", 16, 9], _
    ["Gruppe 2", 34, 7], _
    ["Gruppe 2", 122, 9], _
    ["Gruppe 1", 35, 0], _
    ["Gruppe 3", 19, 6]]

    [/autoit] [autoit][/autoit] [autoit]

    Dim $aI[3]
    $aI[0] = 0
    $aI[1] = 1
    $aI[2] = 2

    [/autoit] [autoit][/autoit] [autoit]

    _ArraySort_MultiColumn($avArray, $aI)
    _ArrayDisplay($avArray, 'After:')

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# =============================================================================
    ; Name.............: _ArraySort_MultiColumn
    ; Description ...: sorts an array at given colums (multi colum sort)
    ; Syntax...........: _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices)
    ; Parameters ...: $aSort - array to sort
    ; $aIndices - array with colum indices which should be sorted in specified order - zero based
    ; $oDir/$iDir - sort direction - if set to 1, sort descendingly else ascendingly
    ; Author .........: UEZ
    ; Version ........: v0.70 build 2013-11-20 Beta
    ; =========================================================================================
    Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0)
    If Not IsArray($aIndices) Or Not IsArray($aSort) Then Return SetError(1, 0, 0) ;checks if $aIndices is an array
    If UBound($aIndices) > UBound($aSort, 2) Then Return SetError(2, 0, 0) ;check if $aIndices array is greater the $aSort array
    Local $1st, $2nd, $x, $j, $k, $l = 0
    For $x = 0 To UBound($aIndices) - 1 ;check if array content makes sense
    If Not IsInt($aIndices[$x]) Then Return SetError(3, 0, 0) ;array content is not numeric
    Next
    If UBound($aIndices) = 1 Then Return _ArraySort($aSort, $oDir, 0, 0, $aIndices[0]) ;check if only one index is given
    _ArraySort($aSort, $oDir, 0, 0, $aIndices[0])
    Do
    $1st = $aIndices[$l]
    $2nd = $aIndices[$l + 1]
    $j = 0
    $k = 1
    While $k < UBound($aSort)
    If $aSort[$j][$1st] <> $aSort[$k][$1st] Then
    If $k - $j > 1 Then
    _ArraySort($aSort, $iDir , $j, $k - 1, $2nd)
    $j = $k
    Else
    $j = $k
    EndIf
    EndIf
    $k += 1
    WEnd
    If $k - $j > 1 Then _ArraySort($aSort, $iDir, $j, $k, $2nd)
    $l += 1
    Until $l = UBound($aIndices) - 1
    Return 1
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    Einmal editiert, zuletzt von UEZ (20. November 2013 um 23:41)