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:

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:

Sort function in Power Apps

Power Apps Formula

ClearCollect(MyFirstCollection,
{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”.

Sort function in Power Apps

Step 3: Add the controls Text label to it and bind it to the corresponding value from the collection to it.

Sort function in Power Apps
Sort function in Power Apps

Step 4: Set the Items property of the gallery control to the following powerapps formula.

Power Apps Formula

Sort(MyFirstCollection, Marks)
Sort function in Power Apps

Step 5: Add another Text label control to add it above the gallery, change its Text to Student Details.

Sort function in Power Apps

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, ... ] )

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

ClearCollect(MyFirstCollection,
{FirstName: "Ashish", LastName: "Goel", Marks: 90},
{FirstName: "Abhishek", LastName: "Bachhan", Marks: 40},
{FirstName: "Ayesha", LastName: "Sharma", Marks: 45},
{FirstName: "Priya", LastName: "Verma", Marks: 67}
) 
SortByColumns function in Canvas Power Apps

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

SortByColumns(MyFirstCollection, "Marks")
SortByColumns function in Canvas Power Apps

Step 3: Add another Text label control to add it above the gallery, change its Text to Student Details.

SortByColumns function in Canvas Power Apps