cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JonathanGibbs
Advocate III
Advocate III

Test for equality between collections

Is there a way to test whether two collections are equal (i.e. have the same content, regardless of order)?

If(Collection1 = Collection2, ...) doesn't work (invalid argument type, which makes sense I guess).

I'm trying this:

UpdateContext({JSON1: JSON(Collection1),JSON2: JSON(Collection2)});
If(JSON1 = JSON2, ....)
This does appear to work, but I'm not sure if the JSON text could be guaranteed always to parse the same way.
Is there a more elegant solution than using JSON?
2 ACCEPTED SOLUTIONS

Accepted Solutions
Maurits
Resolver III
Resolver III

If the collections are not very big with both number of fields and number of records, I would use the Concat function to check if the content is equal.

Eg below, I have two collections (colA and colB) with the same fields and same data, but in different order.
I create a variable with all content of colA using the Concat function.
The same for colB.
And then just compare if the two variables are the same or not (in this example my variable varColA_eq_ColB which is True or False).
There must be better ways, so I keep an eye on this post.

ClearCollect(colA,
{FieldA: 100, FieldB: "ABC"},
{FieldA: 200, FieldB: "DEF"},
{FieldA: 300, FieldB: "GHI"}
);

ClearCollect(colB,
{FieldA: 300, FieldB: "GHI"},
{FieldA: 200, FieldB: "DEF"},
{FieldA: 100, FieldB: "ABC"}
);

UpdateContext({varContentColA: Concat(SortByColumns(colA,"FieldA",Ascending,"FieldB",Ascending),FieldA&"_"&FieldB&";")});
UpdateContext({varContentColB: Concat(SortByColumns(colB,"FieldA",Ascending,"FieldB",Ascending),FieldA&"_"&FieldB&";")});
UpdateContext({varColA_eq_ColB: varContentColA = varContentColB})

View solution in original post

@RandyHayes 

@mdevaney 

@Eelman 

@Maurits 

Thanks for responses and sorry for delay picking up

 

After some testing I've found that the JSON method successfully orders columns (thanks @RandyHayes for this), but takes rows as they come. So you have to order the rows, provided you have a unique identifier column somewhere.

 

I think this makes the JSON method a bit less cumbersome than the CONCAT method. This works:

 

ClearCollect(Collection1, { ColA: 1, ColB: 2 }, { ColA: 3, ColB: 4 });
ClearCollect(Collection2, { ColB: 4, ColA: 3 }, { ColB: 2, ColA: 1 });
UpdateContext( { JSON1: JSON(Sort(Collection1, ColA ) ), JSON2: JSON(Sort(Collection2, ColA ) ) });
If( JSON1 = JSON2, [do something....])
 
It also copes nicely with null values (where a column is present in one row but not another). So I'll stick with sorted JSON, I think!

View solution in original post

6 REPLIES 6
mdevaney
Super User
Super User

@JonathanGibbs 
Can you give an example of the collections you are trying to compare?  This info would be great for building a proof-of-concept app.

Maurits
Resolver III
Resolver III

If the collections are not very big with both number of fields and number of records, I would use the Concat function to check if the content is equal.

Eg below, I have two collections (colA and colB) with the same fields and same data, but in different order.
I create a variable with all content of colA using the Concat function.
The same for colB.
And then just compare if the two variables are the same or not (in this example my variable varColA_eq_ColB which is True or False).
There must be better ways, so I keep an eye on this post.

ClearCollect(colA,
{FieldA: 100, FieldB: "ABC"},
{FieldA: 200, FieldB: "DEF"},
{FieldA: 300, FieldB: "GHI"}
);

ClearCollect(colB,
{FieldA: 300, FieldB: "GHI"},
{FieldA: 200, FieldB: "DEF"},
{FieldA: 100, FieldB: "ABC"}
);

UpdateContext({varContentColA: Concat(SortByColumns(colA,"FieldA",Ascending,"FieldB",Ascending),FieldA&"_"&FieldB&";")});
UpdateContext({varContentColB: Concat(SortByColumns(colB,"FieldA",Ascending,"FieldB",Ascending),FieldA&"_"&FieldB&";")});
UpdateContext({varColA_eq_ColB: varContentColA = varContentColB})

Eelman
Community Champion
Community Champion

@JonathanGibbs I was also needing a similar solution awhile ago and recall coming across this thread. The solution offered by @RandyHayes is similar to that of @Maurits 's. 

 

As Matt has also said, having this as a PowerApps feature would be very helpful!

@JonathanGibbs 

Here's the solution for the collections you provided.  You must sort the collections and convert them to text strings in order to make the comparison like some of the others have been saying.

 

Put this in the Text property label of a label to see how it works.

Concat(Sort(Sort(colA, FieldA, Ascending), FieldB), "")=Concat(Sort(Sort(colB, FieldA, Ascending), FieldB), "")

 

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

@Eelman 

Yes, the concat function works pretty well for doing a "controlled" comparison.

However, I have actually used in several apps, the JSON method as @JonathanGibbs originally posted.  For me it has worked well.  But, you have to make sure that you are in control of the data in the collection.  If you have shaped it yourself, then it is pretty safe.  JSON will sort by field name so they will compare well.

If you have pulled a collection from a datasource directly (like SharePoint) all bets are off as you will have extra information in the collection that you will need to either drop out or otherwise handle (i.e. Attachments, Person columns, etc.)

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@RandyHayes 

@mdevaney 

@Eelman 

@Maurits 

Thanks for responses and sorry for delay picking up

 

After some testing I've found that the JSON method successfully orders columns (thanks @RandyHayes for this), but takes rows as they come. So you have to order the rows, provided you have a unique identifier column somewhere.

 

I think this makes the JSON method a bit less cumbersome than the CONCAT method. This works:

 

ClearCollect(Collection1, { ColA: 1, ColB: 2 }, { ColA: 3, ColB: 4 });
ClearCollect(Collection2, { ColB: 4, ColA: 3 }, { ColB: 2, ColA: 1 });
UpdateContext( { JSON1: JSON(Sort(Collection1, ColA ) ), JSON2: JSON(Sort(Collection2, ColA ) ) });
If( JSON1 = JSON2, [do something....])
 
It also copes nicely with null values (where a column is present in one row but not another). So I'll stick with sorted JSON, I think!

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,000)