Friday, May 13, 2022

Power Automate: Sort An Array

Hello Friends.

Welcome back with another post on Power Automate. Have you ever tried to sort an array in Power Automate. Yes, Sorting. You surprised because, there is no direct function or action provided in Power Automate to sort an array. Well, today, we will look for  work around for the same.

Here, we will use an array of emails and will try to sort alphabetically. Let's start.

  1. I am using an Instant cloud flow with trigger as "Manually trigger a flow".
  2. Define an array "arrayUnsortedEmails".
  3. Now, we need 4 variables-
    1. arrayFilteredEmails- It will be used to hold the filtered emails every time our loop completes one round .
    2. arraySortedEmails- It will be used to hold the final sorted array of emails and will be assigned at the end of flow.
    3. strSmallestEmail- It will be used to hold an email id which will be compared with the emails residing in arrayFilteredEmails and if any email is smaller than this value then the variable will update it's value with the new one.
    4. intRowsCount- It will be used to hold the total number of emails captured in arrayUnsortedEmails array.
    5. intRowIndex- it is a counter variable which will be used for loop execution.
  4. Let's continue and initialize all the above variables.
  5. Now, we will use a Do-until loop where, we will-
    1. Extract the first email from arrayFilteredEmails.
    2. Apply a loop on arrayFilterEmails and check if any other email is smaller (alphabetically sort) than the first email. If Yes, then update the strSmallestEmail with the new email and continue the verification.
    3. Once Apply to each loop ends, add the value of strSmallestEmail into arraySortedEmails.
    4. Filter the arrayFilteredEmails and exclude this smallest email.
    5. Assign the output to arrayFilteredEmail.
    6. Increment the intRowIndex with 1.
    7. The Do-until loop continues the same process again and again till index reaches to the count.
    8. Once the Do-until execution completes, we will get the sorted array.
  6. Now, if you wish to see the output, you may assign the output to a new array variable.
  7. Let's define an array named "arrayFinalOutput" and assign it the value of arraySortedEmails.
  8. All done. Save the flow and test it.
  9. I will show you the initial array and the final array.
  10. Initial Array-
  11. Final Array-
  12. Let me show you the data in notepad-
  13. This way you can implement sorting upon arrays in Power Automate.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.