How to Sort Power Apps Collection [Complete tutorial]

This Power Apps tutorial will go over how to sort the Power Apps collection based on various scenarios.

We recently received a request to sort a collection within Power Apps while working with the Power Apps Canvas app.

It will also show you how to sort a Power Apps collection, which includes the following items:

  • How to sort a Power Apps collection?
  • How to sort the Power Apps collection in descending?
  • How to sort the Power Apps collection by multiple columns?
  • How to sort and filter the Power Apps collection?
  • How to sort the Power Apps collection by date?
  • Power Apps sort collection from SharePoint list
  • How to sort a distinct Power Apps collection?
  • How to sort the choice column within the Power Apps collection?
  • Power Apps sort collection multiple conditions
  • How to sort Power Apps nested collection?
  • Power Apps sort existing column
  • PowerApps sort collection gallery by alphabetically
  • Power Apps sort collection gallery by date

At times, we may need to sort the items or data in a specific order, such as descending or ascending. Similarly, we’ll look at how to sort collection items within Power Apps based on various scenarios.

For sorting, the syntax is:

Sort(Source, expression, order)

Where,

  1. Source: It specifies the table to be sorted.
  2. expression: It specifies an expression that is evaluated over the rows in the source and returns values for sorting.
  3. order: Optional. It specifies the sorting order, either ascending or descending. By default, it sorts the items in ascending order.

How to sort a Power Apps collection?

First, we will see how to sort the items within the Power Apps collection. For this, we will create a Power Apps collection, then sort it, and the steps are as follows:

  • On the Power Apps screen, add a button control and set the Text property (Ex- Create Collection).
  • Insert the below expression on the button’s OnSelect property to create the collection while the user clicks on the button.
OnSelect = Collect(colBook, {Name:"Adventures of Tom Sawyer", Author:"Mark Twain"}, {Name:"Agni Veena", Author:"Kazi Nasrul Islam"}, {Name:"Time Machine", Author:"H.G. Wells"}, {Name:"A passage to India", Author:"E.M.Forster"}, {Name:"Utopia", Author:"Sir Thomas Moor"}, {Name:"The Wild Iris", Author:"Louis Gluck"}, {Name:"Divine Comedy", Author:"Dante"}, {Name:"Bisarjan", Author:"R.N.Tagore"}, {Name:"Geetanjali", Author:"Rabindra Nath Tagore"}, {Name:"Das Kapital", Author:"Karl Marx"})

Where,

  1. colBook is the name of the collection.
  2. Name and Author is the name of the collection headers.
Power Apps collection sort
Power Apps collection sort
  • Add a data table control to the screen and connect that with the collection.
  • Add the fields to the data table to dispaly the collected data.
  • Click in teh button while clicking on the the Alt key to create the collection as well as to dispaly those data in the data table.
PowerApps collection sort
PowerApps collection sort
  • On the above collection, let’s sort the Name column in ascending order. insert the below expression on the data table’s Items property.
Items = Sort(colBook,Name)

We can see, once the formula is applied the data will be sorted in ascending order by default within the data tabbe shown below:

Power Apps sort collection alphabetically
Power Apps sort collection alphabetically

This is how to sort Power Apps collection alphabetically.

Read How to remove all items from a collection in PowerApps

Power Apps sort collection descending

Similarly, we will see how to sort the Power Apps collection in descending order in this section. Let’s use the above Power Apps collection as an example to arrange the items in descending order.

For this, insert the below expression on the data table’s Items property:

Itemss = Sort(colBook,Name,Descending)

Once the formula is inserted in the above data table, we can see the data will display in the descending order as shown below:

Power Apps sort collection descending
Power Apps sort collection descending

This is how to sort the Power Apps collection in descending order.

See also  How to move emails to folder after 30 days using Power Automate

Power Apps sort collection by multiple columns

Have you tried sorting multiple columns in the Power Apps collection? If not, we’ll see how to sort the Power Apps collection by multiple columns in this example.

Suppose, we are going to use the above Power Apps collection where we will add another column Sale.

PowerApps sort collection by multiple columns
PowerApps sort collection by multiple columns

On the above Power Apps collection, sort the Name in descending order and Sale in ascending order.

To sort the collection by multiple columns in Power Apps, add anthor button control to the Power Apps screen and insert the below expression on the button’s OnSelect property.

OnSelect = ClearCollect(SortCollBook, SortByColumns(colBook,"Name",Descending,"Sale",Ascending))

Where, SortCollBook is the name of the new collection to strore the collected data in sorted order.

Next, link the collection to the Power Apps data table and add the fields to it. When the button is pressed, the data is displayed in sorted order, as shown below:

How to sort PowerApps collection by multiple columns
How to sort PowerApps collection by multiple columns

This is how to sort the PowerApps collection by multiple columns.

Read How to remove items from a collection in Power Apps

Power Apps sort collection and filter

Do you want to sort and filter a collection of Power Apps? If so, we recently published a Power Apps collection in which we briefly explained how to sort a filtered Power Apps collection using a relative example. To get the answer, we recommend clicking on the link provided.

Power Apps sort collection by date

In this section, we will see how to sort a Power Apps collection by date. Suppose, there is a SharePoint list having a date column shown below.

Power Apps sort collection by date
Power Apps sort collection by date

Let’s build a Power Apps Collection using this above SharePoint list and sort that collection by date column in descending order. For this, the folloing steps are:

  • On the Power Apps screen, add a button control.
  • Insert the below expression on the button’s OnSelect property to create the Power Apps collection from the SharePoint list.
OnSelect = Collect(collProduct, 'Product Model')

Where,

  1. collProduct is the name of the Power Apps collection
  2. ‘Product Model’ is the name of the SharePoint list.
  • Add a data table to the Power Apps screen and connect that data table to the collection i.e., collProduct. One we click on the button, the data will retrieved collected data will display in the data table.
Sort Power Apps collection by date
Sort Power Apps collection by date
  • To sort the date column on the above data table, update the below expression on the data table’s Items property.
Items = Sort(collProduct,'Purchase Date',Descending)
How to sort Power Apps collection by date
How to sort Power Apps collection by date

This is how to sort the Power Apps collection by date.

Read Power Apps Create Collection Using Excel

Power Apps sort collection from SharePoint list

In this section, we will see how to sort a Power Apps collection that builds from a SharePoint list. Let’s take the above example of the SharePoint list and build a collection on the screen’s visible time. For this, insert the below expression on the Power Apps screen’s OnVisible Property.

OnVisible = Collect(ColProductModel, 'Product Model')

Where,

  • ColProductModel is the name of the collection to store the data from the SharePoint list.
  • ‘Product Model’ is the name of the SharePoint list.
PowerApps sort collection from SharePoint list
PowerApps sort collection from SharePoint list

We can see the collection when the screen is visible. Let’s display this collection via a data table control.

PowerApps sort collection from SharePoint
PowerApps sort collection from SharePoint

Now we will sort the above collection in ascending order based on the Title field. For this, insert the below expression on the data table’s Items property.

Items = Sort(ColProductModel, Title, Ascending)

Once the formula is applied, we can see that the items will be displayed in ascending order shown below:

Power Apps sort collection from SharePoint
Power Apps sort collection from SharePoint

This is how to sort a Power Apps collection from the SharePoint list.

Power Apps sort collection choice column

In this example, we will see how to sort the choice column within the Power Apps collection. Assuming we are going to use the above example of a collection that builds from the SharePoint list, we can see there is a choice column named Manufacturer within the Power Apps collection shown below:

Power Apps sort collection choice column
Power Apps sort collection choice column

Now, we will sort the above-mentioned choice column in ascending order within the Power Apps collection and display those collected data in a data table.

See also  Power Apps Line Chart - How To Build It

For this, insert the below expression within the above Power Apps data table’s Items property.

Items = Sort(collProduct,Manufacturer.Value,Ascending)

As “Manufacturer” is the name of the choice column, so it is denoted as “Manufacturer.Value

PowerApps sort collection choice column
PowerApps sort collection choice column

This is how to sort a choice column within the Power Apps collection.

Read Power Apps Timer Control Examples

Power Apps sort collection multiple conditions

In this section, we will see how to sort a Power Apps collection based on multiple conditions. For this, we are going to use the above SharePoint list, where we will sort the above Power Apps collection based on multiple conditions.

Consider the following scenario: we will only display data whose product value is “Laptop” or “Mouse” and the purchase date is before 11/10/2022. We will sort the filtered data in descending order once it has been filtered.

For this the following steps are:

  • Insert the below expression on the above data table’s Items property to display the items that satisfy the multiple conditions and sort them in descending order.
Items = Sort(Filter('Product Model', Product = "Mouse" Or Product = "Laptop"  And  'Purchase Date' < Date(2022,11,10)), Title, Descending)

We can see the items will be displayed in descending order based on the Titles within the data table as shown below:

PowerApps sort collection multiple conditions
PowerApps sort collection multiple conditions

This is how to sort a Power Apps collection based on multiple conditions.

Power Apps sort distinct collection

In this section, we will see how to work with Power Apps to sort distinct collections. That means we will sort a Power Apps collection that will display only the distinct or unique values. Suppose we have a Power Apps collection that displays some repeated data within a data table control, as shown below:

Power Apps sort distinct collection
Power Apps sort distinct collection

Let’s sort the above data table that will display only the distinct values. To do this, the following steps are:

  • Add another button control to the Power Apps screen.
  • Set the Text property as per the requirement.
  • Insert the following expression into the OnSelect property of the button to create a new collection to store the distinct sorted values.
OnSelect = ClearCollect(UniqueColCountry, Sort(Distinct(ColCountry,Country),Result,Ascending))

Where UniqueColCountry is the name of the new collection.

PowerApps sort distinct collection
PowerApps sort distinct collection
  • Add another data table control to the Power Apps screen. Set the Items property with the new collection i.e., UniqueColCountry. Also, add the field to display the unique values.

Once the button is clicked, we can see the data table will display the unique values as shown below.

How to sort distinct Power Apps collection
How to sort distinct Power Apps collection

This is how to sort a distinct collection within the Power Apps.

Read Power Apps Notify() function

Power Apps sort nested collection

In this section, we will see how to sort a nested collection within the Power Apps. For this, we will create a nested collection within the Power Apps. To create this, the following steps are:

  • On the Power Apps screen, add a button control and insert the below expression on the OnSelect property to create the nested collection.
OnSelect = ClearCollect(
    ColOrder,
    {
        CustomerName: "Jack",
        OrderCol: {
            OrderID: 101,
            OrderName: "Laptop",
            Quantity: 123
        }
    },
    {
        CustomerName: "Albert",
        OrderCol: {
            OrderID: 122,
            OrderName: "Desktop",
            Quantity: 203
        }
    },
    {
        CustomerName: "Mark",
        OrderCol: {
            OrderID: 253,
            OrderName: "Mouse",
            Quantity: 159
        }
    },
    {
        CustomerName: "Lucy",
        OrderCol: {
            OrderID: 369,
            OrderName: "Keyboard",
            Quantity: 114
        }
    }
)

Where,

  1. ColOrder is the name of the parent collection.
  2. OrderCol is the name of the child collection.
  3. OrderID, OrderName, and Quantity are the names of the collection headers.
Power Apps sort nested collection
Power Apps sort nested collection

Let’s click on the button while clicking on the Alt key. We can see, it will create a nested collection within the Power Apps collection section.

PowerApps sort nested collection
PowerApps sort nested collection

Let’s create another Power Apps collection to sort this nested collection in ascending order. For this, add another button control to the Power Apps screen and insert the below expression on the button’s OnSelect property.

OnSelect = ClearCollect(CollSort,Sort(ColOrder,CustomerName))

Where,

  • CollSort is the name of the new collection to store the sorted items.
  • ColOrder is the name of the existing collection that we have created.
  • CustomerName is the name of the collection header.
How to sort nested PowerApps collection
How to sort nested PowerApps collection

As per the above expression, we can see the nested collection will be sorted in ascending order shown as below:

Sort nested PowerApps collection
Sort nested PowerApps collection

This is how to sort a nested collection within PowerApps.

See also  Power Apps Cascading Dropdown Control

Read Power Apps Filter With Date Picker

Power Apps sort existing collection

In the earlier examples, we saw how to sort the collection after it was created. But now we’ll look at how to sort the existing collection within the Power Apps we’ve already built.

Suppose, we have an existing collection named CollCandidate that displays some candidate’s names and age shown below:

Power Apps sort existing collection
Power Apps sort existing collection

Let’s sort the above existing collection in ascending order by their age. For this, the following steps are:

  • Add a button control to the Power Apps screen.
  • Insert the below formula to the button’s OnSelect property to sort the collection while the user clicks on the button.
OnSelect = ClearCollect(ColCandidate, Sort(ColCandidate, Age, Ascending))

Where,

  1. ColCandidate is the name of the existing collection.
  2. Age is the name of the collection column to sort.
  3. Ascending defines the order of sorting.
Power Apps sort collection ascending
Power Apps sort collection ascending

Once the button is clicked, we can see that the existing collection will be sorted in ascending order shown as below:

PowerApps sort existing collection
PowerApps sort existing collection

This is how to sort an existing Power Apps collection.

PowerApps sort collection gallery

In this section, we will see how to sort a Power Apps collection using different types of columns within the gallery. That means we will sort the Power Apps collection via a gallery by a text field, and a date field.

To work with this, we have prepared a Power Apps collection using the above-specified SharePoint column and displayed that collection within the Power Apps gallery.

Power Apps sort collection gallery
Power Apps sort collection gallery

Let’s perform the sort operation in different columns within the gallery.

Read How to disable Power Apps date picker

Power Apps sort collection gallery by alphabetically

To sort the Title column (i.e., Model) alphabetically with the above gallery, the following steps are:

  • Add a sort icon to the screen and place it beside the Model.
  • Insert the below expression on the icon’s OnSelect property.
OnSelect = UpdateContext({SortColumn: "Title", SortDescending: !SortDescending})

Where,

  • SortColumn and SortDescending are the names of the update context variables.
  • Title” and !SortDescending are the values of the variables.
Power Apps sort collection gallery by alphabetically
Power Apps sort collection gallery by alphabetically

Power Apps sort collection gallery by date

Similarly, to sort the above Power Apps collection gallery by date, the following steps are:

  • Add a sort icon to the screen and place it beside the Date column (i.e., Purchase Date).
  • Insert the below expression on the icon’s OnSelect property.
OnSelect = UpdateContext({Column: "PurchaseDate", SortOrder: !SortOrder})

where PurchaseDate is the internal name of the data source column.

Power Apps sort collection gallery by date
Power Apps sort collection gallery by date

Finally, insert the below expression on the gallery’s items property to sort the items in descending and ascending order.

Items = SortByColumns(collProduct,Column,If(SortOrder,Ascending,Descending))

where collProduct is the Power Apps collection’s name.

Power Apps collection sort items in gallery
Power Apps collection sort items in gallery

Let’s preview the app. We can see that when we click on the first sort icon, it will sort the Title column within the gallery as shown below:

PowerApps collection sort items in gallery
PowerApps collection sort items in gallery

Similarly, when we click on the second sort icon, it will sort the date column in either descending or ascending order.

PowerApps collection sort gallery by date
PowerApps collection sort gallery by date

This is how to perform sort operation on the Power Apps collection within the gallery based on different columns.

Conclusion

From this Power Apps Tutorial, we have learned all about how to sort the Power Apps collection based on different scenarios such as:

  • Power Apps sort collection alphabetically
  • Power Apps sort collection in descending
  • Power Apps sort collection by multiple columns
  • Power Apps sort collection and filter
  • Power Apps sort collection by date
  • Power Apps sort collection from SharePoint list
  • Power Apps sort distinct collection
  • Power Apps sort collection choice column
  • Power Apps sort collection multiple conditions
  • Power Apps sort nested collection
  • Power Apps sort existing column
  • PowerApps sort collection gallery by alphabetically
  • Power Apps sort collection gallery by date

You may like the following Power Apps tutorials:

>