Hi,
There are two arrays with type Array<Dictionary<String,Any>>.
and the two array are not sorted, so even if they have a same contents, the order in array is not the same maybe.
I want to compare two arrays and figure out they have same contents.
simple example.
Array A is [a, b, c] and Array [b, c, a] , I want to get true in boolean. (true return)
[a, b, c] and [a, b, c, d] are regarded as not same array. (false retun)
surely, [a, b, c] and [x, g, t] are regarded as not same array too. (false return)
Is there any way to do this compare ?
Thanks 😝
You'll need to pick apart the various pieces of your complex data type and check for equivalence.
Break down your structure into pieces. For example, you'll need to first see if your 'Any' type is equivalent. I would also ask if you need to use 'Any' or if you can use a more specific type? If using a specific type, your work will be easier.
Beyond the type check, I see three things you'll need to do:
- Compare the count of array items. If they are different, return false
- Perhaps convert the Dictionary<String, Any> into a sorted array of tuples where each tuple contains a String-Any pair. Then, put those sorted arrays into a main array and sort that. i.e. each Array<Dictionary<String, Any>> is turned into a structure that is completely sorted.
- Iterate through the index of both arrays (you've already proven they hold the same number of items) and use == on their elements. You'll need to write a custom implementation of == to work with those elemnts (array of tuples).