Sort and SortByColumns Power Apps functions
1. Sort Power Apps function
The Sort function is used to sort a table based on a formula. The formula is evaluated for each record of the table, and the results are used to sort the table. The formula must result in a number, a string, or a Boolean value; it can't result in a table or a record.
Power Apps Syntax a) First Syntax Sort(Table, Formula [, SortOrder ] ) b) Second Syntax Sort (Table, column-name [,SortOrder])
The function has the following parameters:
- Table - It is a required parameter. It specifies the Table (i.e., any collection sharepoint list) to sort.
- Formula - It is a required parameter. This formula is evaluated for each record of the table, and the results are used to sort the table. We can reference columns within the table.
- column-name - Here we can specify the column name on which the table is sorted.
- SortOrder - It is an optional parameter. We have two sord order Ascending and descending. Specify SortOrder.Descending to sort the table in descending order. SortOrder.Ascending is the default value.
Example:Step 1: Select the + Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following formula:
Power Apps Formula
{FirstName: "Ashish", LastName: "Goel", Marks: 90},
{FirstName: "Abhishek", LastName: "Bachhan", Marks: 40},
{FirstName: "Ayesha", LastName: "Sharma", Marks: 45},
{FirstName: "Priya", LastName: "Verma", Marks: 67}
)
Step 2: Add a Blank Vertical gallery control, and set its Items property to “MyFirstCollection”.
Step 3: Add the controls Text label to it and bind it to the corresponding value from the collection to it.
Step 4: Set the Items property of the gallery control to the following powerapps formula.
Power Apps Formula
Step 5: Add another Text label control to add it above the gallery, change its Text to Student Details.
To sort first by one column and then by another, we embed a Sort formula within another. For example, you can use this formula to sort a Contacts table first by a LastName column and then by a FirstName column: Sort( Sort( Contacts, LastName ), FirstName )
2. SortByColumns Power Apps function
The SortByColumns function can also be used to sort a table based on one or more columns. The parameter list for SortByColumns provides the names of the columns to sort by and the sort direction per column. Sorting is performed in the order of the parameters (sorted first by the first column, then the second, and so on). Column names are specified as strings, requiring double quotes if directly included in the parameter list.
We can combine SortByColumns with a Drop down or List box control to enable users to select which column to sort by.
Power Apps Syntax SortByColumns( Table, ColumnName1 [, SortOrder1, ColumnName2, SortOrder2, ... ] )
- Table - Required. Table to sort.
- ColumnName(s) - Required. The column names to sort on, as strings.
- SortOrder(s) - Optional. SortOrder.Ascending or SortOrder.Descending. SortOrder.Ascending is the default. If multiple ColumnNames are supplied, all columns except the last column must include a SortOrder.
Example: Demonstration of the SortByColumns function in Power Apps. Step 1: Select the + Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following formula:
Power Apps Formula
{FirstName: "Ashish", LastName: "Goel", Marks: 90},
{FirstName: "Abhishek", LastName: "Bachhan", Marks: 40},
{FirstName: "Ayesha", LastName: "Sharma", Marks: 45},
{FirstName: "Priya", LastName: "Verma", Marks: 67}
)
Step 2: Add a vertical gallery control, and add the controls Text label to it and bind it to the corresponding value from the collection to it.
Power Apps Formula
Step 3: Add another Text label control to add it above the gallery, change its Text to Student Details.