Function Sort_TwoDimensionBubble(TempArray As Variant, _
Optional iElement As Integer = 1, _ Optional iDimension As Integer = 1, _ Optional bAscOrder As Boolean = True) As Boolean Dim arrTemp As Variant Dim i%, j% Dim NoExchanges As Integer On Error Goto Error_BubbleSort 'Loop until no more "exchanges" are made. If iDimension% = 1 Then ReDim arrTemp(1, UBound(TempArray, 2)) Else ReDim arrTemp(UBound(TempArray, 1), 1) End If Do NoExchanges = True 'Loop through each element in the array If iDimension% = 1 Then For i% = LBound(TempArray, iDimension%) To _ UBound(TempArray, iDimension%) - 1 'If the element is greater than the element 'following it, exchange the two elements If (bAscOrder And (TempArray(i%, iElement%) > TempArray _ (i% + 1,iElement%))) Or (Not bAscOrder And _ (TempArray(i%, iElement%) < _ TempArray(i% + 1, iElement%))) Then NoExchanges = False For j% = LBound(TempArray, 2) To UBound(TempArray, 2) arrTemp(1, j%) = TempArray(i%, j%) Next j% For j% = LBound(TempArray, 2) To UBound(TempArray, 2) TempArray(i%, j%) = TempArray(i% + 1, j%) Next j% For j% = LBound(TempArray, 2) To UBound(TempArray, 2) TempArray(i% + 1, j%) = arrTemp(1, j%) Next j% End If Next i% Else For i% = LBound(TempArray, iDimension%) To _ UBound(TempArray, iDimension%) - 1 'If the element is greater than the element 'following it, exchange the two elements. If (bAscOrder And (TempArray(iElement%, i%) > TempArray _ (iElement%, i% + 1))) Or (Not bAscOrder And _ (TempArray(iElement%, i%) < _ TempArray(iElement%, i% + 1))) Then NoExchanges = False For j% = LBound(TempArray, 1) To UBound(TempArray, 1) arrTemp(j%, 1) = TempArray(j%, i%) Next j% For j% = LBound(TempArray, 1) To UBound(TempArray, 1) TempArray(j%, i%) = TempArray(j%, i% + 1) Next j% For j% = LBound(TempArray, 1) To UBound(TempArray, 1) TempArray(j%, i% + 1) = arrTemp(j%, 1) Next j% End If Next i% End If Loop While Not (NoExchanges) Sort_TwoDimensionBubble = True On Error Goto 0 Exit Function Error_BubbleSort: On Error Goto 0 Sort_TwoDimensionBubble = False End Function Inputs: TempArrayVariant iElementInteger iDimension Integer bAscOrderBoolean Returns: Boolean if the sort was successful Assumes: Best used for smaller arrays, since the bubblesort algorithm is not suited to very large arrays |