;------------------------------------------------------------------------------------------------------------
;	Function		_ArraySort_2ary(ByRef $ARRAY [, $DIM_1ST=0 [, $DESCENDING=0 [$REVERSE=False]]])
;
;	Description		sort an 2D-Array 2-ary
;					BaseIndex is 0
;					sort the whole array
;
;	Parameter		$ARRAY:			Array to sort
;		optional	$DIM_1ST:		MainSortIndex; 1st Dim. [0] or last occurence in 2nd Dim.[all other values] (default 0)
;		optional	$DESCENDING:	Sort ascending[0]/descending[1] (default 0)
;		optional	$REVERSE:		Sort 2nd Dimension reverse to 1st Dimension (default False)
;
;	Return			Succes			ByRef 2-ary sorted Array
;					Failure			0  set @error
;									@error = 1 	given array is not array
;									@error = 2 	given array has only 1 dimension
;
;	Requirements	By using numeric entry, be sure that type is "number" for correct sort
;					Works with any occurences in 2nd Dimension
;					
;	Author			BugFix (bugfix@autoit.de)
;------------------------------------------------------------------------------------------------------------
#include <Array.au3>
Func _ArraySort_2ary(ByRef $ARRAY, $DIM_1ST=0, $DESCENDING=0, $REVERSE=False)
	If ( Not IsArray($ARRAY) ) Then
		SetError(1)
		Return 0
	EndIf
	Local $FIRST = 0, $LAST, $tmpFIRST, $sortYES = 0
	Local $UBound2nd = UBound($ARRAY,2)
	If @error = 2 Then
		SetError(2)
		Return 0
	EndIf
	If $DIM_1ST <> 0 Then $DIM_1ST = $UBound2nd-1
	Local $arTmp[1][$UBound2nd]
	_ArraySort($ARRAY,$DESCENDING,0,0,$UBound2nd,$DIM_1ST)
	If $REVERSE Then
		Switch $DESCENDING
			Case 0
				$DESCENDING = 1
			Case 1
				$DESCENDING = 0
		EndSwitch
	EndIf
	For $u = 0 To $UBound2nd-1
		For $i = 0 To UBound($ARRAY)-1
			If $sortYES = 0 Then
				If $u > 0 Then
					If ( $i < UBound($ARRAY)-1 ) And ( $ARRAY[$i][$u] = $ARRAY[$i+1][$u] ) And _
						( $ARRAY[$i][$u-1] = $ARRAY[$i+1][$u-1] )Then
						$sortYES = 1
						$FIRST = $i
					EndIf
				Else
					If ( $i < UBound($ARRAY)-1 ) And ( $ARRAY[$i][$u] = $ARRAY[$i+1][$u] ) Then
						$sortYES = 1
						$FIRST = $i
					EndIf
				EndIf
			ElseIf $sortYES = 1 Then
				If ( $i = UBound($ARRAY)-1 ) Or ( $ARRAY[$i][$u] <> $ARRAY[$i+1][$u] ) Then 
					$sortYES = 0
					$LAST = $i +1
					ReDim $arTmp[$LAST-$FIRST][$UBound2nd]
					$tmpFIRST = $FIRST
					For $k = 0 To UBound($arTmp)-1 
						For $l = 0 To $UBound2nd-1
							$arTmp[$k][$l] = $ARRAY[$tmpFIRST][$l]
						Next
						$tmpFIRST += 1
					Next
					$tmpFIRST = $FIRST
					Switch $DIM_1ST
						Case 0
							If $u = $UBound2nd-1 Then
								_ArraySort($arTmp,$DESCENDING,0,0,$UBound2nd,$UBound2nd-1)
							Else
								_ArraySort($arTmp,$DESCENDING,0,0,$UBound2nd,$u+1)
							EndIf
							For $k = 0 To UBound($arTmp)-1
								For $l = 1 To $UBound2nd-1
									$ARRAY[$tmpFIRST][$l] = $arTmp[$k][$l]
								Next
								$tmpFIRST += 1
							Next					
						Case $UBound2nd-1
							If $u = $UBound2nd-1 Then
								_ArraySort($arTmp,$DESCENDING,0,0,$UBound2nd,0)
							Else
								_ArraySort($arTmp,$DESCENDING,0,0,$UBound2nd,$UBound2nd-1-$u-1)
							EndIf
							For $k = 0 To UBound($arTmp)-1
								For $l = 0 To $UBound2nd-2
									$ARRAY[$tmpFIRST][$l] = $arTmp[$k][$l]
								Next
								$tmpFIRST += 1
							Next				
					EndSwitch
				EndIf
			EndIf
		Next
		$sortYES = 0
	Next
EndFunc ;==>_ArraySort_2ary	