' ------------------------------------------------------------------------------------- ' Name: SortMultiArray ' Author: Markus Diersbock ' Created: 06/01/2004 ' ' Description: When a multidimensional array and it's index are passed in, ' the function will sort the values referenced in ' SortElement and return the associated sorted index. ' ' Arguments: aryIn Multidimensional array to be sorted ' IndexElement Index to return when sorting on SortElement ' SortElement Elements to be sorted ' SortASC If set to TRUE, sorting order will be ascending ' ' Returns: Array with Index sorted ' ' Notes: Unlike other sort functions, this function doesn't ' manipulate the actual passed in array, it simply passes back ' the order of the sorted index. ' ------------------------------------------------------------------------------------- Function SortMultiArray(ByVal aryIn As Variant, ByVal IndexElement As Integer, ByVal SortElement As Integer, ByVal SortASC As Boolean) As Variant On Error GoTo Catch Dim i As Long, x As Long Dim aryTemp(1, 1) As Variant Dim SortDir As Integer Dim aryHold() As Variant Dim aryRtn() As Variant Dim lbnd As Long dim ubnd As Long ReDim aryHold(2, UBound(aryIn, 2)) If SortASC = True Then SortDir = -1 Else SortDir = 1 End If ' Populate temp array with Index and Sort elements For i = LBound(aryIn, 2) To UBound(aryIn, 2) aryHold(0, i) = aryIn(IndexElement, i) aryHold(1, i) = aryIn(SortElement, i) Next ' Get array's boundries lbnd = LBound(aryHold, 2) ubnd = UBound(aryHold, 2) - 1 ' Bubble sort temp array For i = lbnd to ubnd For x = lbnd to ubnd If StrComp(aryHold(SortElement, i), aryHold(SortElement, x), vbTextCompare) = SortDir Then aryTemp(0, 0) = aryHold(IndexElement, i) aryTemp(1, 0) = aryHold(SortElement, i) aryHold(IndexElement, i) = aryHold(IndexElement, x) aryHold(SortElement, i) = aryHold(SortElement, x) aryHold(IndexElement, x) = aryTemp(0, 0) aryHold(SortElement, x) = aryTemp(1, 0) End If Next Next ReDim aryRtn(UBound(aryIn, 2)) ' Only return array with sorted indexes For i = LBound(aryHold, 2) To UBound(aryHold, 2) aryRtn(i) = aryHold(0, i) Next SortMultiArray = aryRtn Finally: ' clean up code here. Exit Function Catch: ' Error tracking code here Resume Finally End Function