<img src="//trc.taboola.com/1222697/log/3/unip?en=page_view" width="0" height="0" style="display:none">


The Knowledgebase articles are deprecated. Please visit the new Guides and Resources here: https://www.uipath.com/developers/guides-and-resources.

Knowledge Base

How to compare Collections

A collection is a unit that groups multiple elements. The best example is an array. To make it simple, collections are used to store groups of items that serve one purpose.

In this example, we will discuss the step-by-step process on how to compare two collections.



1. To compare collections, we will start with comparing the number of items. If this number differs, there is no need for any further comparisons.

2. If the number of elements is the same, we need to compare each item from the first collection with the corresponding item in the second collection.


Sample Workflow

1. Setting up the two Collections:

 Let's start with setting up two collections in Assign Activity. The 2 variables should be declared as an array or in this case IEnumerable<Int32>, which is specific to an array of integers.

2. The first Decision:

The first Decision checks if the number of items for both collections are equal. This condition is declared: firstCollection.count = secondCollection.count

If the Decision gives a False result, meaning the items are NOT equal, then it will give a message: "Collections have a different number of items".

If the Decision gives a True result, then it will move to a process which compares each item in both collections.


3. Comparing Collections:

3.1. Setting the Counter. Remember that the indexing of items in an array starts with 0 and then continues, ascending. So in this example, the indexing starts at 0 and ends at 3.


3.2. Found Different. This sets up whether the loop execution will continue or not. Once there is a difference from the item in the Collection 1 to Collection 2, this will trigger an end to the loop and will send a "Collections have a different number of items" message.


3.3. The Loop. The start of the comparison is done by using the While Activity.


3.4. The Condition. The conditions counter < firstCollection.Count and (Not foundDifferent) establishes whether the execution of the loop continues or ends.


3.5. The If Activity. This is where the actual comparison takes place.

This statement, firstCollection(counter) <> secondCollection(counter) checks if the corresponding item from the first Collection is different from the second collection. If they are equal, then it will continue with the loop and will proceed to the next item. This is triggered by counter=counter+1.


4. The Result. The final result will just be a confirmation message saying whether the 2 collections are the same or not.