- Offizieller Beitrag
Wir werden allen Wünschen gerecht.
Hier also die UDF um Arrays nach Länge der Einträge zu sortieren.
- Sortierung wahlweise auf/absteigend
- bei 2D Sortierung nach 1. oder 2. Spalte
Spoiler anzeigen
#include <array.au3>
Dim $ar[5], $ar2[5][2]
$ar[0] = "otto"
$ar[1] = "aha"
$ar[2] = "Adebar"
$ar[3] = "b"
$ar[4] = "ah"
$ar2[0][0] = "otto"
$ar2[1][0] = "aha"
$ar2[2][0] = "Adebar"
$ar2[3][0] = "b"
$ar2[4][0] = "ah"
$ar2[0][1] = "c"
$ar2[1][1] = "ha"
$ar2[2][1] = "barracuda"
$ar2[3][1] = "bor"
$ar2[4][1] = "aha"
_ArrayDisplay($ar, "Array 1D, unsortiert")
_ArraySortByLen($ar)
_ArrayDisplay($ar, "Array 1D, sortiert nach Länge Eintrag")
_ArrayDisplay($ar2, "Array 2D, unsortiert")
_ArraySortByLen($ar2,0,2,0)
_ArrayDisplay($ar2, "Array 2D, sortiert 1. Spalte nach Länge Eintrag")
_ArraySortByLen($ar2,0,2,1)
_ArrayDisplay($ar2, "Array 2D, sortiert 2. Spalte nach Länge Eintrag")
;----------------------------------------------------------------------------------------------------------------------
; Function _ArraySortByLen(ByRef $ARRAY [, $iDESCENDING=0 [, $iDIM=0[ , $iSORT=0]]])
;
; Description - Sorts an 1D/2D Array by Length.
; - BaseIndex is 0; sorts the whole array.
;
; Parameter $ARRAY: Array to sort
; 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 by Length
;
; Requirements Only 2 occurences in the second dimension
; #include <array.au3>
;
; Author BugFix ([email='bugfix@autoit.de'][/email])
;----------------------------------------------------------------------------------------------------------------------
Func _ArraySortByLen(ByRef $ARRAY, $iDESCENDING=0, $iDIM=0, $iSORT=0)
Local $arTmp1D[1][1], $arTmp2D[1][1]
If $iDIM = 0 Then $iDIM = 1
Switch $iDIM
Case 1 ; 1D
ReDim $arTmp1D[UBound($ARRAY)][2]
For $i = 0 To UBound($ARRAY)-1
$arTmp1D[$i][0] = StringLen($ARRAY[$i])
$arTmp1D[$i][1] = $ARRAY[$i]
$ARRAY[$i] = ''
Next
_ArraySort($arTmp1D,$iDESCENDING,0,0,2,0)
For $i = 0 To UBound($arTmp1D)-1
$ARRAY[$i] = $arTmp1D[$i][1]
Next
Case 2 ; 2D
ReDim $arTmp2D[UBound($ARRAY)][3]
For $i = 0 To UBound($ARRAY)-1
$arTmp2D[$i][2] = StringLen($ARRAY[$i][$iSORT])
$arTmp2D[$i][0] = $ARRAY[$i][0]
$arTmp2D[$i][1] = $ARRAY[$i][1]
$ARRAY[$i][0] = ''
$ARRAY[$i][1] = ''
Next
_ArraySort($arTmp2D,$iDESCENDING,0,0,3,2)
For $i = 0 To UBound($arTmp2D)-1
$ARRAY[$i][0] = $arTmp2D[$i][0]
$ARRAY[$i][1] = $arTmp2D[$i][1]
Next
EndSwitch
EndFunc ; ==>_ArraySortByLen
Edit:
Habe auch diese Funktion auf beliebige Größe der 2.ten Dimension erweitert, d.h. es wird bei gleicher Länge von Elementen in Spalte 1, innerhalb dieser Elemente nach Spalte 2 usw. sortiert.
Füge sie einfach als neue Funktion _Array2DSortByLen mit an:
Spoiler anzeigen
;----------------------------------------------------------------------------------------------------------------------
; Function _Array2DSortByLen(ByRef $ARRAY [, $iDESCENDING=0])
;
; Description - Sorts an 1D/2D Array by Length.
; - BaseIndex is 0; sorts the whole array.
;
; Parameter $ARRAY: Array to sort
; optional $iDESCENDING: Sort ascending[0]/descending[1] (default 0)
;
; Return Succes -1 ByRef sorted Array by Length
; Failure 0 set @error = 1; no array
;
; Requirements Func _ArraySort_2ary()
; #include <array.au3>
;
; Author BugFix ([email='bugfix@autoit.de'][/email])
;----------------------------------------------------------------------------------------------------------------------
Func _Array2DSortByLen(ByRef $ARRAY, $iDESCENDING=0)
If ( Not IsArray($ARRAY) ) Then
SetError(1)
Return 0
EndIf
If $iDESCENDING <> 0 Then $iDESCENDING = 1
Local $UBound2nd = UBound($ARRAY,2)
Local $arTmp[1] = ['']
If @error = 2 Then
ReDim $arTmp[UBound($ARRAY)][2]
For $i = 0 To UBound($ARRAY)-1
$arTmp[$i][0] = StringLen($ARRAY[$i])
$arTmp[$i][1] = $ARRAY[$i]
$ARRAY[$i] = ''
Next
_ArraySort($arTmp,$iDESCENDING,0,0,2,0)
For $i = 0 To UBound($arTmp)-1
$ARRAY[$i] = $arTmp[$i][1]
Next
Else
ReDim $arTmp[UBound($ARRAY)][$UBound2nd+1]
For $i = 0 To UBound($ARRAY)-1
For $k = 0 To $UBound2nd-1
$arTmp[$i][$k] = StringLen($ARRAY[$i][$k])
Next
$arTmp[$i][$UBound2nd] = $i
Next
_ArraySort_2ary($arTmp, 0, $iDESCENDING)
For $i = 0 To UBound($arTmp)-1
For $k = 0 To $UBound2nd-1
$arTmp[$i][$k] = $ARRAY[$arTmp[$i][$UBound2nd]][$k]
Next
Next
ReDim $arTmp[UBound($ARRAY)][$UBound2nd]
$ARRAY = $arTmp
EndIf
Return -1
EndFunc ;==>_ArraySortByLen