Salesforce introduced the Collection Sort element back in the Summer '21 release. It is possible to use this element to reorder the items within a collection by multiple fields. Moreover, it is possible to limit the collection after sorting. Here is how and when to use this element.
How to Use the Collection Sort Element
Add this element and select any collection variable that you want. Then, choose the fields that you want to sort by. If the collection variable has more than one field, you can click Add Sort Option to sort by multiple fields. The order of the fields matters in this case. Therefore, make sure that you add the fields in order of greater to lesser priority. You can sort by up to 3 fields at a time.
If you mark "Put empty string and null values first", it puts the records with an empty or null value in the Sort By field at the start of the collection. Otherwise, they will be at the end.
The second purpose of this element is about limiting the collection. It is possible to keep all items or limit the collection. In order to limit the collection, choose "Set the maximum number of items" and enter a number. Pay attention that this is the maximum number of items. For instance, if your collection has 5 items and you want to keep 10 items, it won't fail. It will keep those 5 items.
Importance of the Element
While Collection Sort isn't used much compared to other tools in flows, it can be really handy in specific cases. Although it is possible to perform sorting directly within the Get Records element, it is not possible to sort by multiple fields. On the other hand, if the collection is not created via Get Records element, you cannot use the Get Records element to sort the collection. Therefore, you have to use the Collection Sort element in order to sort the items.
In SOQL, LIMIT is a very crucial clause that is used in many use cases. Even though it is not possible to limit a collection directly from the Get Records element, you can do it using the Collection Sort element. This makes it a very useful element in Salesforce Flow.
Unlike the Collection Filter element, Collection Sort element makes the changes directly in the selected collection variable. It means that this element overrides the selected collection variable. Therefore, it is important to place this element in the correct position within the flow. For instance, if you limit a collection and choose to keep 10 items, the collection itself will have 10 items and the rest will be removed. If you need to check the size of the initial collection, you must do it before the Collection Sort element.
This element works with collections and the output is always a collection. This means that if you limit the collection to 1 item, it will still be a collection that has 1 item. Therefore, you will need to use Loop and Assignment elements to assign that item to a single variable.
Let's build a screen flow that displays a data table of top 10 closed won opportunity records and the number of closed won opportunity records.
1- Add a Get Records element to find the closed won opportunity records.
2- Add an Assignment element to assign the size of the collection to a number variable. It is important to do it before the Collection Sort element, otherwise you cannot find the correct record count.
3- Add a Collection Sort element to sort the collection and limit it to 10 items.
4- Add a Screen element with a data table to display the records.
Then, add a display text to display the number of the opportunities.
At the end, your flow should look like this.
Here is the result of the flow. As you can see, there are 14 closed won opportunity records and the data table is displaying the top 10.
Here is what happens if you put the Collection Sort element before the Assignment element. As you can see, the data table displays the top 10 records. However, it doesn't display the correct number of closed won opportunity records. It is because the Collection Sort element overrides the collection variable.
Read this post to see a little bit more complicated example.