For one of my projects I needed a function to compare two arrays. The following function returns true if both array have the same elements (regardless of their position in arrays).
Function ArraysAreEqual (vA As Variant, vB As Variant) As Variant
Dim IsEqual As Variant
ArraysAreEqual = True
Forall a In vA
IsEqual = False
Forall b In vB
If a = b Then
IsEqual = True
Exit Forall
End If
End Forall
If Not IsEqual Then
ArraysAreEqual = False
Exit Function
End If
End Forall
Forall b In vB
IsEqual = False
Forall a In vA
If b = a Then
IsEqual = True
Exit Forall
End If
End Forall
If Not IsEqual Then
ArraysAreEqual = False
Exit Function
End If
End Forall
End Function
To execute your code only when both array are different simply use
If Not (ArraysAreEqual ( mBefore, mAfter )) Then
' your code goes here
' ...
End If
Thanks for sharing your code.
I just wonder if there is any particular reason to define the return variable as variant. I would suggest using integer or boolean instead. Is that for the sake of backward compatibility or is there any other reason?
Function ArraysAreEqual (vA As Variant, vB As Variant) As Variant
Dim rez As Variant
rez = Evaluate({@implode(@sort(@Explode(“} & Join(vA, “~”) & Join(vB, “~”) )
if rez(0) = 0 then ArraysAreEqual = false else ArraysAreEqual = true
end function
try my code, my approach more interesting, on my opinion 🙂
the idea is simple, we sort our arrays and convert them to string, then we will compare two string and will get the result.
advantage of my approach:
– more simple, just 1 line;
– faster;
– we can compare any number of arrays
Dmytro, thanks for sharing.
Hi
This formula is not working… even brackets for @sort and all missed.