# Compare Two Arrays

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
' ...

End If
```

## 5 thoughts on “Compare Two Arrays”

1. Mirek Navratil

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?

2. Dmytro

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

3. 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.