Array 1D/2D sortieren und Dubletten löschen

    • Offizieller Beitrag

    Wenn man z.B. aus einer Datei Begriffe selektiert, ist es sehr wahrscheinlich, dass auch mehrfach derselbe Begriff erscheint.

    Mit dieser Funktion lassen sich 1- und 2-Dimensionale Arrays sortieren und gleichzeitig werden die Dubletten entfernt.
    Sind die Daten vom Typ String, kann ich wahlweise auch auf Gross-/Kleinschreibung prüfen.

    Hier mit Bsp.:

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    Dim $ar[10]
    Dim $ar2D[10][2]

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

    $ar[0] = "Otto"
    $ar[1] = "otto"
    $ar[2] = "Bernd"
    $ar[3] = "Kurt"
    $ar[4] = "otto"
    $ar[5] = "Kurt"
    $ar[6] = "Bernd"
    $ar[7] = "Otto"
    $ar[8] = "Kurt"
    $ar[9] = "bernd"

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

    $ar2 = $ar

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

    $ar2D[0][0] = "Otto"
    $ar2D[1][0] = "Otto"
    $ar2D[2][0] = "Bernd"
    $ar2D[3][0] = "Kurt"
    $ar2D[4][0] = "otto"
    $ar2D[5][0] = "Kurt"
    $ar2D[6][0] = "Bernd"
    $ar2D[7][0] = "Otto"
    $ar2D[8][0] = "Kurt"
    $ar2D[9][0] = "bernd"

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

    $ar2D[0][1] = "meier"
    $ar2D[1][1] = "meier"
    $ar2D[2][1] = "McBee"
    $ar2D[3][1] = "Becker"
    $ar2D[4][1] = "Meier"
    $ar2D[5][1] = "Schramm"
    $ar2D[6][1] = "McBee"
    $ar2D[7][1] = "Lorenz"
    $ar2D[8][1] = "meier"
    $ar2D[9][1] = "Gantzer"

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

    $ar2D2 = $ar2D

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

    _ArrayDisplay($ar, "unsortiert 1D")

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

    _ArraySortDblDel($ar)
    _ArrayDisplay($ar, "o. case sens 1D")

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

    _ArraySortDblDel($ar2,1)
    _ArrayDisplay($ar2, "m. case sens 1D")

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

    _ArrayDisplay2D($ar2D, "unsortiert 2D")

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

    _ArraySortDblDel($ar2D,0,0,2,1)
    _ArrayDisplay2D($ar2D, "o. case sens 2D")

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

    _ArraySortDblDel($ar2D2,1,0,2,1)
    _ArrayDisplay2D($ar2D2, "m. case sens 2D")

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

    ;----------------------------------------------------------------------------------------------------------------------
    ; Function _ArraySortDblDel(ByRef $ARRAY [, $CASESENS=0 [, $iDESCENDING=0 [, $iDIM=0[ , $iSORT=0]]]])
    ;
    ; Description - Sorts an 1D/2D Array and delete double entries (2D -> combination by '[n][0]' and '[n][1]').
    ; - By using string, you can choose case sensitivity.
    ; - BaseIndex is 0; sorts the whole array.
    ;
    ; Parameter $ARRAY: Array to sort
    ; optional $CASESENS: Case sensitivity off[0] or on[1] (default 0)
    ; optional $iDESCENDING: Sort ascending[0]/descending[1] (default 0)
    ; optional $iDIM: Occurences in the second dimension, eg $A[100]=0 or $A[100][2]=2 (default 0)
    ; optional $iSORT: SortIndex by using 2 dimensions (default 0)
    ;
    ; Return ByRef sorted Array without doubles
    ;
    ; Requirements Only 2 occurences in the second dimension
    ; #include <array.au3>
    ;
    ; Author BugFix ([email='bugfix@autoit.de'][/email])
    ;----------------------------------------------------------------------------------------------------------------------

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

    Func _ArraySortDblDel(ByRef $ARRAY, $CASESENS=0, $iDESCENDING=0, $iDIM=0, $iSORT=0)
    Local $arTmp1D[1], $arTmp2D[1][2], $dbl = 0
    $arTmp1D[0] = ""
    $arTmp2D[0][0] = ""
    If $iDIM = 0 Then $iDIM = 1
    _ArraySort($ARRAY,$iDESCENDING,0,0,$iDIM,$iSORT)
    Switch $iDIM
    Case 1 ; 1D
    For $i = 0 To UBound($ARRAY)-1
    $dbl = 0
    For $k = 0 To UBound($arTmp1D)-1
    Switch $CASESENS
    Case 0
    If $arTmp1D[$k] = $ARRAY[$i] Then $dbl = 1
    Case 1
    If $arTmp1D[$k] == $ARRAY[$i] Then $dbl = 1
    EndSwitch
    Next
    If $dbl = 0 Then
    If $arTmp1D[0] = "" Then
    $arTmp1D[0] = $ARRAY[$i]
    Else
    _ArrayAdd($arTmp1D, $ARRAY[$i])
    EndIf
    Else
    $dbl = 0
    EndIf
    Next
    $ARRAY = $arTmp1D
    Case 2 ; 2D
    For $i = 0 To UBound($ARRAY)-1
    $dbl = 0
    For $k = 0 To UBound($arTmp2D)-1
    Switch $CASESENS
    Case 0
    If ( $arTmp2D[$k][0] = $ARRAY[$i][0] ) And _
    ( $arTmp2D[$k][1] = $ARRAY[$i][1] ) Then $dbl = 1
    Case 1
    If ( $arTmp2D[$k][0] == $ARRAY[$i][0] ) And _
    ( $arTmp2D[$k][1] == $ARRAY[$i][1] ) Then $dbl = 1
    EndSwitch
    Next
    If $dbl = 0 Then
    If $arTmp2D[0][0] = "" Then
    $arTmp2D[0][0] = $ARRAY[$i][0]
    $arTmp2D[0][1] = $ARRAY[$i][1]
    Else
    ReDim $arTmp2D[UBound($arTmp2D)+1][2]
    $arTmp2D[UBound($arTmp2D)-1][0] = $ARRAY[$i][0]
    $arTmp2D[UBound($arTmp2D)-1][1] = $ARRAY[$i][1]
    EndIf
    Else
    $dbl = 0
    EndIf
    Next
    $ARRAY = $arTmp2D
    EndSwitch
    EndFunc ; ==>_ArraySortDblDel

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

    Func _ArrayDisplay2D($Array, $Title="")
    Local $str = ""
    For $i = 0 To UBound($Array)-1
    If $i = 0 Then
    $str &= "[" & $i & "] = " & $Array[$i][0] & " | " & $Array[$i][1]
    Else
    $str &= @LF & "[" & $i & "] = " & $Array[$i][0] & " | " & $Array[$i][1]
    EndIf
    Next
    MsgBox(0, $Title, $str)
    EndFunc

    [/autoit]