Recently, I have done a task on how to sort a Gallery control in Power Apps using different real-time scenarios.
In this article, we will see what is Power Apps Sort function, Power Apps SortByColumns function, and many more:
- Power Apps Sort Gallery By Column
- Power Apps Sort Gallery By Multiple Columns
- Power Apps Sort Gallery By Ascending Descending
- Power Apps Sort Gallery By ID
- Power Apps Sort Gallery By Newest
- Power Apps Sort Gallery By Button
- Power Apps Sort Gallery By Lookup Column
- Power Apps Sort Gallery By Choice Column
- Power Apps Sort Gallery By Date
- Power Apps Sort Gallery By Month
- Power Apps Sort Gallery By Calculated Field
- Power Apps Sort and Filter
Power Apps Sort Function
The Power Apps sort function sorts a table based on the user’s formula. In Power Apps, we can sort the table or its items in Ascending or Descending order (User’s choice).
Syntax:
Sort( Table, Formula [, SortOrder ] )
Where,
- Table – You can specify the table to sort
- Formula – This formula is evaluated for each record of the table
- SortOrder – It is optional. You can specify SortOrder.Descending to sort the table in descending order. SortOrder.Ascending is the default value
Power Apps SortByColumns Function
Similarly, the Power Apps SortByColumns function also sorts the table based on single or multiple columns.
Syntax:
SortByColumns( Table, ColumnName1 [, SortOrder1, ColumnName2, SortOrder2, ... ] )
Where,
- Table -You can specify the table to sort
- ColumnName(s) – It is required. You can sort column names as strings
- SortOrder(s) – It is optional, SortOrder.Ascending is the default. If multiple ColumnNames are supplied and the last column must include a SortOrder
Power Apps Sort Gallery By Column
Now, we will discuss how to sort the Power Apps gallery by column using a simple scenario.
Scenario:
I have a SharePoint list named “Vacation Budget” having the below fields.
Column Name | Data Type |
Destinaton | It is a default single line of text |
Expense Name | A single line of text |
Category | Choice |
Total Cost | Number |
Start Date | Date and time |
End Date | Date and time |
Now, I would like to sort these SharePoint list records and display the result on the Gallery control, as shown below.
Output:
To work around this, follow the below steps. Such as:
1. On the Power Apps Screen, insert a Gallery control and set its Items property to the code below.
Items = Sort(
'Vacation Budget',
Title,
SortOrder.Ascending
)
Where,
- ‘Vacation Budget’ = SharePoint Online list
- Title = SharePoint list title field
2. Once your app is ready, Save, Publish, and Preview the app. The gallery will display each record from the SharePoint Online list based on the ascending order, as shown below.
This way, we can sort the Power Apps gallery by column.
Power Apps Sort Gallery By multiple columns
In this example, I will show you how to sort the Power Apps gallery by multiple columns. To do so, select the gallery control and set its Items property to the code below.
Items = SortByColumns(
'Vacation Budget',
"Title",
SortOrder.Descending,
"TotalCost",
SortOrder.Ascending
)
Where,
- “Title”, “TotalCost” = SharePoint list fields
This is how to sort Power Apps gallery control by multiple columns.
Power Apps Sort Gallery By Ascending Descending
Now, I want to sort the Power Apps gallery Alphabetically [Ascending or Descending] using my SharePoint Online list [Vacation Budget].How to Sort Dropdown Choices Alphabetically in Power Apps
Output:
To do so, follow the below-mentioned steps. Such as:
1. On the Power Apps Screen, insert a Gallery control and set its Items property as:
Items = Sort(
'Vacation Budget',
'Expense Name', //For Ascending Order
SortOrder.Ascending
)
2. Similarly, select the gallery control and set its Items property to the code below.
Items = Sort(
'Vacation Budget',
'Expense Name', //For Descending Order
SortOrder.Descending
)
In the same way, if you want to display sorted gallery records based on the selecting sort icon [Either ascending or descending], you can follow the below steps.
3. On the Power Apps Screen, insert a Sort icon and set its OnSelect property as:
OnSelect = Set(
varColumn,
"Title"
);
Set(
varOrder,
If(
varOrder = SortOrder.Ascending,
SortOrder.Descending,
SortOrder.Ascending
)
)
Where,
- varColumn = 1st Global Variable Name
- “Title” = SharePoint Title Field
- varOrder = 2nd Global Variable Name
- SortOrder.Descending, SortOrder.Ascending = Sort order can be either ascending or descending
4. Finally, select the gallery control and set its Items property as:
Items =
5. Once your all updates are done, Save, Publish, and Preview the app. The gallery displays sorted records based on the selecting sort icon [Either ascending or descending], as shown below.
This is how we can work with the Power Apps sort gallery by ascending and descending.
Power Apps Sort Gallery By ID
Here, we will see how to sort the Power Apps gallery by ID. In this case, set the Items property of a gallery control using the below code.
Items = SortByColumns(
'Vacation Budget',
"ID",
SortOrder.Descending
)
Where,
- “ID” = SharePoint ID column
Power Apps Sort Gallery By Button
In this section, I will discuss how to work with the Power Apps sort gallery by button. To do so, follow the below steps.
1. On the Power Apps Screen, insert a Button control and set its OnSelect property to the code below.
OnSelect = Set(
varSort,
Sort(
'Vacation Budget',
Title,
SortOrder.Descending
)
)
Where,
- varSort = Power Apps variable name
2. Then, select the Gallery control and set its Items property to the code below.
Items = varSort
3. Once you click the button control, the gallery will display sort records in descending order. Have a look at the below screenshot for the output.
This is how we can work with the Power Apps sort gallery by button.
Power Apps Sort Gallery By Newest
In this section, I will explain how to work with the Power Apps sort gallery by newest. For that, select the gallery control and set its Items property as:
Items = SortByColumns(
'Vacation Budget',
"StartDate",
SortOrder.Descending
)
Where,
- “StartDate” = SharePoint date field
This is how we can sort the Power Apps gallery by newest using descending order.
Power Apps Sort Gallery By Lookup Column
I have a SharePoint list named “Employee Onboarding,” which contains some columns, including the Lookup field [Department].
Input:
Now, I would like to sort the Power Apps gallery by the Lookup column. To do that, insert a Gallery control and set its Items property to the code below.
Items = Sort(
'Employee Onboarding',
Department.Value,
SortOrder.Ascending
)
Where,
- ‘Employee Onboarding’ = SharePoint Online list
- Department = SharePoint Lookup field
This is way, we can sort the Power Apps gallery by Lookup column.
Power Apps Sort Gallery By Choice Column
Suppose you want to sort the Power Apps gallery by choice column, checkout the below post.
Checkout -> Power Apps Sort Gallery By Choice Column
Power Apps Sort Gallery By Date
Are you want to sort the Power Apps gallery by date, you can follow the below post.
Click Here -> Power Apps Sort Gallery By Date
Power Apps Sort Gallery By Month
In the same way, if you want to sort the Power Apps gallery by Month, check out the tutorial below.
Checkout -> Power Apps Sort Gallery By Month
Power Apps Sort and Filter
In the last section, I will explain how to use the Power Apps sort and filter using a simple scenario.
Scenario:
In Power Apps, there is a Gallery control [It retries data from the SharePoint list]. I want to sort the data table in Ascending order and filter the gallery based on the SharePoint choice field value [Category is equal to Entertainment].
To achieve it, follow the below steps. Such as
1. Select the gallery and write the code below on its Items property:
Items = Sort(
Filter(
'Vacation Budget',
Category.Value = "Entertainment"
),
Title,
SortOrder.Ascending
)
Where,
- Category = SharePoint choice field value
This is how we can work with the Power Apps sort and filter.
Some more Power Apps articles you may also like:
- Power Apps Sort Gallery By Day
- Power Apps Sort Distinct Filter
- How to Use Power Apps Find Function
- How to Sort Data Table in Power Apps
- How to Sort Dropdown Choices Alphabetically in Power Apps
- Save Power Apps Current User and Manager Name in SharePoint Person Column
I trust this Power Apps tutorial is helpful for you. If you have any requirements for the Power Apps sort gallery, follow this post until the end, where I explain different examples with real-time scenarios.
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
Great post!
I’d recommend a “conflict” in the two column sort example, to show how it works…