Sort 2 1-dimension arrays
Actually sort 1st array (Array1) based on values ascending or descending, then change items in Array2 to match order in Array1
Sort can be in Ascending or Descending order.
Not like sortArray, this one involves two arrays, and can decide order
Edit 2021-10-12: Adding ability in v2 to ignore case in sorting strings.
Function ArrSort_2Arr(arrShort, ByRef arrSub, Order_1Asc_2Desc, Optional CaseSensative = 1)
Ord1 = 1
If Order_1Asc_2Desc = 2 Then Ord1 = 2
For I = UBound(arrShort) - 1 To 0 Step -1
For j = 0 To I
If CaseSensative = 1 Then
Cond1 = arrShort(j) > arrShort(j + 1)
If Ord1 = 2 Then Cond1 = arrShort(j) < arrShort(j + 1)
Else
Arritem1 = UCase(arrShort(j))
Arritem2 = UCase(arrShort(j + 1))
Cond1 = Arritem1 > Arritem2
If Ord1 = 2 Then Cond1 = Arritem1 < Arritem2
End If
If Cond1 Then
temp = arrShort(j + 1)
temp1 = arrSub(j + 1)
arrSub(j + 1) = arrSub(j)
arrSub(j) = temp1
arrShort(j + 1) = arrShort(j)
arrShort(j) = temp
End If
Next
Next
ArrSort_2Arr = arrShort
End Function
Ord1 = 1
If Order_1Asc_2Desc = 2 Then Ord1 = 2
For I = UBound(arrShort) - 1 To 0 Step -1
For j = 0 To I
If CaseSensative = 1 Then
Cond1 = arrShort(j) > arrShort(j + 1)
If Ord1 = 2 Then Cond1 = arrShort(j) < arrShort(j + 1)
Else
Arritem1 = UCase(arrShort(j))
Arritem2 = UCase(arrShort(j + 1))
Cond1 = Arritem1 > Arritem2
If Ord1 = 2 Then Cond1 = Arritem1 < Arritem2
End If
If Cond1 Then
temp = arrShort(j + 1)
temp1 = arrSub(j + 1)
arrSub(j + 1) = arrSub(j)
arrSub(j) = temp1
arrShort(j + 1) = arrShort(j)
arrShort(j) = temp
End If
Next
Next
ArrSort_2Arr = arrShort
End Function
arrShort, byRef arrSub, Order_1Asc_2Desc
Sort A2 ascending into A3, then re-order items in A1 according to new list
A3 = ArrSort_2Arr(A2, A1, 1)
Sort A2 descending into A4, then re-order items in A1 according to new list
A4 = ArrSort_2Arr(A2, A1, 2)
A3 = ArrSort_2Arr(A2, A1, 1)
Sort A2 descending into A4, then re-order items in A1 according to new list
A4 = ArrSort_2Arr(A2, A1, 2)
Views 5,449
Downloads 1,475
CodeID
DB ID
ANmarAmdeen
614
Revisions
v2.0
Tuesday
October
12
2021