Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Answered

For All Ordering

(0) ShareShare
ReportReport
Posted on by 30
Hi,
 
Is there any way to order/sort a For All operation? In the use case, I need to apply a row number to each row in a collection, sorted alphabetically by one of the values.
 
Kind Regards,
  • Arrow3009 Profile Picture
    Arrow3009 30 on at
    For All Ordering
    @Pstork1 Thanks for the advice below, i'll have a look at using a Flow to achieve this.
     
     
    @ronaldwalcott Thanks for this workaround, this looks like a good solution to achieve this within Power Apps. I'll give this a go.
     
     
     
    Kind Regards,
     
     
  • Verified answer
    Pstork1 Profile Picture
    Pstork1 64,996 on at
    For All Ordering
    The easiest way would be to use a Power Automate flow to do the ordering and then send back the resulting collection.  Flow loops have an option to run concurrently in parallel, but that isn't the standard. So using a flow you can do a true loop.
  • Suggested answer
    ronaldwalcott Profile Picture
    ronaldwalcott 1,501 on at
    For All Ordering
    You can simulate one. Here is an example from my notes that I responded to on a similar type of question. The approach would be the same. The objective was to renumber the column PriorityRank
    Hopefully it helps.
     

    Add a Sequence Column to a Sorted Table

    Depending on your answer you could do something like create a sorted collection with the ID and priority rank (this column is only needed for testing)
    SortByColumns(table, PriorityRank)
    You would then need to add a new PriorityRank column which would need to be the incremented value
    AddColumn(SortByColumns(table, PriorityRank), newPriorityRank, 0)
     
    To do this you use something like
     
    ClearCollect(
        SortedIndexedPriority,
        ForAll(
            Sequence(CountRows(SortedPriority)),
            With(
                {
                    Index: Value,
                    Item: Last(
                        FirstN(SortedPriority, Value)
                    )
                },
                {
                    ItemField1: Item.Name,
                    NewPriorityRank: Index*10,
                    ID: Item.ID,
                    PriorityRank: Item.PriorityRank
                }
            )
        )
    );
     
    You would then use this collection to update the original records
     
  • Arrow3009 Profile Picture
    Arrow3009 30 on at
    For All Ordering
    Hi Paul,
     
    Thanks for the reply, that makes sense considering it isn't a true loop.
     
    Do you know of any workaround whereby we could effectively loop through a collection to achieve this?
     
     
  • Suggested answer
    Pstork1 Profile Picture
    Pstork1 64,996 on at
    For All Ordering
    If you read the documentation you will find that the ForAll() isn't a true loop. It applies whatever formula you supply to all the records simultaneously.  It will often look like its doing things in order if processing the formula takes the same amount of time for each item. But that is just an illusion. There is no order to the way the formula is applied and no way to force one.

    "When writing your formula, keep in mind that records can be processed in any order and, when possible, in parallel. The first record of the table may be processed after the last record."
     
    ----------------------------------------------------------------------------------
    If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!

    Paul Papanek Stork, MVP
    Blog: https://www.dontpapanic.com/blog
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,666

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,996

Leaderboard