View Full Version : how to exchange Multidimesional Arrays' datas?
doniface
12-10-2023, 11:30 PM
Hi, everybody:
I need to exchange array's data when they match the condition, for example I have these datas:
arr(1,1)=2, arr(1,2)="A", arr(1,3)="date1", arr(1,4)="num1"
arr(2,1)=1, arr(2,2)="B", arr(2,3)="date2", arr(2,4)="num2"
and I with compare arr(x,1) value , when the value is smaller, then I need to EXCHANGE all values in the array, just like these below:
arr(1,1)=1, arr(1,2)="B", arr(1,3)="date2", arr(1,4)="num2"
arr(2,1)=2, arr(2,2)="A", arr(2,3)="date1", arr(2,4)="num1"
Now my way to change them is ONE by ONE, just like this
tmpvalue=arr(1,1)
arr(1,1)=arr(2,1)
arr(2,1)=tmpvalue
tmpvalue=arr(1,2)
arr(1,2)=arr(2,2)
arr(2,2)=tmpvalue
.
.
.
.
Is there better way to exchange them more effectively?
jdelano
12-11-2023, 03:47 AM
I got it done this way, to handle any number of items in the array:
Dim arr(3, 4) As Variant
Dim firstIndex As Integer
Dim secondIndex As Integer
Dim dataWasSwapped As Boolean
Dim tempVar As Variant
arr(1, 1) = 2
arr(1, 2) = "A"
arr(1, 3) = "date1"
arr(1, 4) = "num1"
arr(2, 1) = 1
arr(2, 2) = "B"
arr(2, 3) = "date2"
arr(2, 4) = "num2"
arr(3, 1) = 3
arr(3, 2) = "C"
arr(3, 3) = "date3"
arr(3, 4) = "num3"
Debug.Print "before loop"
Debug.Print arr(1, 1), arr(1, 2), arr(1, 3), arr(1, 4)
Debug.Print arr(2, 1), arr(2, 2), arr(2, 3), arr(2, 4)
Debug.Print arr(3, 1), arr(3, 2), arr(3, 3), arr(3, 4)
StartLoopOver:
dataWasSwapped = False
For firstIndex = 1 To UBound(arr, 1)
If firstIndex + 1 > UBound(arr, 1) Then Exit For
If arr(firstIndex, 1) > arr(firstIndex + 1, 1) Then
For secondIndex = 1 To UBound(arr, 2)
' swap the position of the data in the array
tempVar = arr(firstIndex, secondIndex)
arr(firstIndex, secondIndex) = arr(firstIndex + 1, secondIndex)
arr(firstIndex + 1, secondIndex) = tempVar
Next secondIndex
dataWasSwapped = True
End If
If dataWasSwapped Then GoTo StartLoopOver ' when data is moved up - start the process over
Next firstIndex
Debug.Print "after loop"
Debug.Print arr(1, 1), arr(1, 2), arr(1, 3), arr(1, 4)
Debug.Print arr(2, 1), arr(2, 2), arr(2, 3), arr(2, 4)
Debug.Print arr(3, 1), arr(3, 2), arr(3, 3), arr(3, 4)
edit: add spacing to make it a bit more readble
anapest
12-14-2023, 08:01 AM
Yes, there is a more effective way to exchange the values in the array when the condition is met. You can achieve this by directly swapping the entire rows of the array.
geometry dash (https://geometrydash-free.com)
Here's an example of how you can achieve this in a programming language like Python:
```python
# Example array
arr = [
[2, "A", "date1", "num1"],
[1, "B", "date2", "num2"]
]
# Compare arr[x][0] values and swap entire rows if condition is met
if arr[0][0] > arr[1][0]:
arr[0], arr[1] = arr[1], arr[0]
# Output the updated array
print(arr)
```
In this example, the entire rows are swapped using simultaneous assignment, which is a more efficient way to exchange the values in the array based on the specified condition.
arnelgp
12-15-2023, 06:02 AM
if you have large numbers of Items on each array, consider using an ADO.Recordset.
you only need to add Sort order to it.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.